今天 Candy Crush Saga 發表新單元,Level 351 ~ 365 啟動。
剩下十一步,一個條紋糖果在右下角四乘四,
附近又沒有辦法再造另個條紋糖果,這代表一件事,
我得在其他區域再造兩個條紋糖果,再想辦法黏在旁邊。
是該放棄了。
放棄前把糖果炸掉,死也要壯壯烈烈的死去,
刷著刷著,結果掉下來不偏不倚兩個條紋糖果又瞎貓摸耗子黏在旁邊
接著曼尼敲出延長賽兩分砲,不該放棄任何機會。
using System;
using System.Collections.Generic;
namespace TopCoder.GraphPractice
{
class Marketing
{
public long howMany(String[] compete)
{
Dictionary<int, Vertex> graph = GetGraph(compete);
int arrangedCount = 0;
foreach (Vertex vertex in graph.Values)
{
if (!vertex.Visited)
{
vertex.Consumer = ConsumerGroup.Teenagers;
if (HasArrangement(vertex))
{
arrangedCount++;
}
else
{
return -1;
}
}
}
return (long)Math.Pow(2, arrangedCount);
}
private Dictionary<int, Vertex> GetGraph(String[] compete)
{
Dictionary<int, Vertex> graph = new Dictionary<int, Vertex>();
for (int i = 0; i < compete.Length; i++)
{
graph.Add(i, new Vertex());
}
for (int i = 0; i < compete.Length; i++)
{
String[] vertexList = compete[i].Split();
foreach (String vertex in vertexList)
{
if (!String.IsNullOrEmpty(vertex))
{
int j = Int32.Parse(vertex);
if (!graph[i].Neighborhood.ContainsKey(j))
{
graph[i].Neighborhood.Add(j, graph[j]);
}
if (!graph[j].Neighborhood.ContainsKey(i))
{
graph[j].Neighborhood.Add(i, graph[i]);
}
}
}
}
return graph;
}
private bool HasArrangement(Vertex vertex)
{
bool hasArrangement = true;
Stack<Vertex> stack = new Stack<Vertex>();
stack.Push(vertex);
while (stack.Count > 0)
{
Vertex top = stack.Pop();
if (top.Visited)
{
continue;
}
top.Visited = true;
foreach (Vertex neighborhood in top.Neighborhood.Values)
{
if (neighborhood.Consumer == ConsumerGroup.Unknown)
{
if (top.Consumer == ConsumerGroup.Teenagers)
{
neighborhood.Consumer = ConsumerGroup.Adults;
}
else if (top.Consumer == ConsumerGroup.Adults)
{
neighborhood.Consumer = ConsumerGroup.Teenagers;
}
}
else
{
if (top.Consumer == neighborhood.Consumer)
{
hasArrangement = false;
}
}
stack.Push(neighborhood);
}
}
return hasArrangement;
}
private class Vertex
{
public Dictionary<int, Vertex> Neighborhood { get; set; }
public bool Visited { get; set; }
public ConsumerGroup Consumer { get; set; }
public Vertex()
{
Neighborhood = new Dictionary<int, Vertex>();
Visited = false;
Consumer = ConsumerGroup.Unknown;
}
}
private enum ConsumerGroup
{
Unknown,
Adults,
Teenagers
}
}
}
using System;
using System.Collections.Generic;
namespace TopCoder.GraphPractice
{
class grafixMask
{
public int[] sortedAreas(String[] rectangles)
{
bool[,] visited = GetBlockedPixels(rectangles);
List<int> areas = new List<int>();
for (int row = 0; row < 400; row++)
{
for (int col = 0; col < 600; col++)
{
if (!visited[row, col])
{
areas.Add(GetConnectedArea(row, col, ref visited));
}
}
}
areas.Sort();
return areas.ToArray();
}
private bool[,] GetBlockedPixels(String[] rectangles)
{
bool[,] blocked = new bool[400, 600];
foreach (String rectangle in rectangles)
{
String[] coordinates = rectangle.Split();
int topLeftRow = Int32.Parse(coordinates[0]);
int topLeftCol = Int32.Parse(coordinates[1]);
int bottomRightRow = Int32.Parse(coordinates[2]);
int bottomRightCol = Int32.Parse(coordinates[3]);
for (int row = topLeftRow; row <= bottomRightRow; row++)
{
for (int col = topLeftCol; col <= bottomRightCol; col++)
{
blocked[row, col] = true;
}
}
}
return blocked;
}
// Depth first search.
private int GetConnectedArea(int row, int col, ref bool[,] visited)
{
int area = 0;
Stack<Vertex> stack = new Stack<Vertex>();
stack.Push(new Vertex(row, col));
while (stack.Count > 0)
{
Vertex top = stack.Pop();
if (top.Row < 0 || top.Row >= 400)
{
continue;
}
if (top.Col < 0 || top.Col >= 600)
{
continue;
}
if (visited[top.Row, top.Col])
{
continue;
}
visited[top.Row, top.Col] = true;
area++;
stack.Push(new Vertex(top.Row + 1, top.Col));
stack.Push(new Vertex(top.Row - 1, top.Col));
stack.Push(new Vertex(top.Row, top.Col + 1));
stack.Push(new Vertex(top.Row, top.Col - 1));
}
return area;
}
private class Vertex
{
public int Row { get; set; }
public int Col { get; set; }
public Vertex(int row, int col)
{
Row = row;
Col = col;
}
}
}
}