http://poj.org/problem?id=1027
题意:给一个10*15的地图,里面填充R,G,B三种颜色,每次找到当前地图的同色最大区域M,并将其删除,删除M后,上面的小球自然下落,当有空列时,空列右边的小球同时向左移动一列,
当最后地图上同色最大区域的小球个数是1或0时,循环结束;注意输出格式,最后输出一个空行。
思路:
while(true)
{
找到同色最大区域,包括其面积Maxsize,坐标,颜色(BFS,手写队列);
if (Maxsize == 1 || Maxsize == 0) break;
删除该最大区域,即将map[i][j] 置为0,(BFS,手写队列);
重置地图;
输出;
}
1 #include<stdio.h> 2 #include<string.h> 3 char map[20][20]; 4 bool vis[20][20]; 5 int MaxR,MaxC;//分别存同色最大区域左下角坐标 6 int Maxsize;//Maxsize存最大面积 7 char Maxcolor;//最大同色区域的颜色; 8 9 int Area(int i,int j)//搜索当前同色区域面积 10 { 11 struct node 12 { 13 int x,y; 14 }que[160]; 15 16 int head,tail; 17 head = tail = 0; 18 19 que[tail].x = i; 20 que[tail++].y = j; 21 vis[i][j] = true; 22 23 char color = map[i][j]; 24 int size = 0; 25 while(head < tail) 26 { 27 int x = que[head].x; 28 int y = que[head++].y; 29 size++; 30 31 if((x+1)<10 && !vis[x+1][y] && map[x+1][y] == color) 32 { 33 que[tail].x = x+1; 34 que[tail++].y = y; 35 vis[x+1][y] = true; 36 } 37 if((x-1)>=0 && !vis[x-1][y] && map[x-1][y] == color) 38 { 39 que[tail].x = x-1; 40 que[tail++].y = y; 41 vis[x-1][y] = true; 42 } 43 if((y-1)>=0 && !vis[x][y-1] && map[x][y-1] == color) 44 { 45 que[tail].x = x; 46 que[tail++].y = y-1; 47 vis[x][y-1] = true; 48 } 49 if((y+1)<15 && !vis[x][y+1] && map[x][y+1] == color) 50 { 51 que[tail].x = x; 52 que[tail++].y = y+1; 53 vis[x][y+1] = true; 54 } 55 } 56 return size; 57 } 58 59 void SearchArea()//搜索同色最大区域; 60 { 61 int i,j; 62 memset(vis,false,sizeof(vis)); 63 Maxsize = 0; 64 for(j = 0; j < 15; j++) 65 { 66 for(i = 0; i < 10; i++) 67 { 68 int size = -1; 69 if(!vis[i][j] && map[i][j]) 70 { 71 size = Area(i,j); 72 if(size > Maxsize) 73 { 74 Maxsize = size; 75 MaxR = i; 76 MaxC = j; 77 Maxcolor = map[MaxR][MaxC]; 78 } 79 } 80 } 81 } 82 } 83 84 85 void DelArea()//删除最大区域 86 { 87 struct node 88 { 89 int x,y; 90 }que[160]; 91 int head,tail; 92 head = tail = 0; 93 que[tail].x = MaxR; 94 que[tail++].y = MaxC; 95 96 Maxcolor = map[MaxR][MaxC]; 97 map[MaxR][MaxC] = 0; 98 99 while(head < tail) 100 { 101 int x = que[head].x; 102 int y = que[head++].y; 103 map[x][y] = 0; 104 105 if(x+1 < 10 && map[x+1][y] == Maxcolor) 106 { 107 map[x+1][y] = 0; 108 que[tail].x = x+1; 109 que[tail++].y = y; 110 } 111 if(x-1 >= 0 && map[x-1][y] == Maxcolor) 112 { 113 map[x-1][y] = 0; 114 que[tail].x = x-1; 115 que[tail++].y = y; 116 } 117 if(y-1 >= 0 && map[x][y-1] == Maxcolor) 118 { 119 map[x][y-1] = 0; 120 que[tail].x = x; 121 que[tail++].y = y-1; 122 } 123 if(y+1 < 15 && map[x][y+1] == Maxcolor) 124 { 125 map[x][y+1] = 0; 126 que[tail].x = x; 127 que[tail++].y = y+1; 128 } 129 } 130 } 131 132 void NewMap()//重置 133 { 134 bool empty[15] = {false};//记录哪一列是空列 135 int i,j; 136 for(j = 0; j < 15; j++)//重置行 137 { 138 int ti = -1;//表示第ti行是空的,ti = -1 表示现在没有空行; 139 bool flag = false; 140 for(i = 0; i < 10; i++) 141 { 142 if(map[i][j]) 143 { 144 flag = true; 145 if(ti != -1) 146 { 147 map[ti][j] = map[i][j];//将第i行移至第ti行 148 map[i][j] = 0;//将第i行置0; 149 i = ti;//从ti行开始再一次枚举; 150 ti = -1; 151 } 152 } 153 else 154 { 155 ti = i; 156 while(i+1 < 10 && !map[i+1][j]) 157 i++; 158 } 159 } 160 if(!flag) 161 empty[j] = true; 162 } 163 int tj = -1;//表示第tj列是空的,tj = -1说明现在好没有空列 164 for(j = 0; j < 15; j++)//重置列 165 { 166 if(!empty[j])//如果第j列是不空的 167 { 168 if(tj != -1) 169 { 170 for(i = 0; i < 10; i++) 171 { 172 map[i][tj] = map[i][j]; 173 map[i][j] = 0; 174 } 175 empty[j] = true; 176 j = tj; 177 tj = -1; 178 } 179 } 180 else 181 { 182 tj = j; 183 while(j+1<15 && empty[j+1]) 184 j++; 185 } 186 } 187 } 188 189 int main() 190 { 191 int test; 192 scanf("%d",&test); 193 for(int item = 1; item <= test; item++) 194 { 195 for(int i = 9; i >= 0; i--) 196 scanf("%s",map[i]); 197 198 printf("Game %d: ",item); 199 200 int Balls = 150; 201 int step = 0; 202 int sumscore = 0; 203 while(true) 204 { 205 Maxsize = -1; 206 SearchArea();//寻找同色最大区域; 207 if(Maxsize == 0 || Maxsize == 1) 208 break; 209 210 DelArea();//删除当前最大区域 211 NewMap();//重置 212 213 int score = (Maxsize-2)*(Maxsize-2); 214 printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points. ",++step,MaxR+1,MaxC+1,Maxsize,Maxcolor,score); 215 Balls = Balls - Maxsize; 216 sumscore += score; 217 } 218 if(Balls == 0) 219 sumscore += 1000; 220 printf("Final score: %d, with %d balls remaining. ",sumscore,Balls); 221 } 222 return 0; 223 }