Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
1A。暴力,0ms#include <iostream> #include <stdio.h> using namespace std; int trans[2][7][2] = {{{-1,0},{-1,1},{0,-1},{0,1},{1,0},{1,1},{0,0}}, {{-1,-1},{-1,0},{0,-1},{0,1},{1,-1},{1,0},{0,0}}}; struct pt{ int x,y; }; pt bfsList[1010][110]; int main() { int T; scanf("%d", &T); for(int ii = 0; ii < T;ii++){ int N, M; scanf("%d%d", &N, &M); int bricks[2][15][15] = {0}; for(int i = 1; i <= N; i++){ char str[15]; scanf("%s", str); for(int j = 1; j <= M; j++){ char temp = str[j-1]; if(temp == 'H') bricks[0][i][j] = 1; else bricks[0][i][j] = -1; } } if(N == 1){ bool hasCold = 0; for(int i = 1; i <= M; i++){ if(bricks[0][1][i] == -1){ hasCold = 1; break; } } if(hasCold) printf("1\n"); else printf("impossible\n"); continue; } int bfsNum[1010] = {0}; int minTime; bool ky = 0; for(int i = 1; i <= M; i++){ if(bricks[0][1][i] == -1){ bfsList[0][bfsNum[0]].x = 1; bfsList[0][bfsNum[0]].y = i; bfsNum[0]++; } } //首先计算下一秒的地圖 for(int t = 1; t <= 999; t++){ int from = (t+1)%2, to = t%2; for(int i = 1; i <= N; i++){ for(int j = 1; j <= M; j++){ int cnt = 0; for(int k = 0; k < 6; k++){ if(bricks[from][i+trans[i%2][k][0]][j+trans[i%2][k][1]] == -1){ cnt++; } } if(bricks[from][i][j] == 1){ if(cnt == 3) bricks[to][i][j] = -1; else bricks[to][i][j] = 1; } else{ if(cnt == 2 || cnt == 3){ bricks[to][i][j] = -1; } else bricks[to][i][j] = 1; } } } bool used[15][15] = {0}; for(int i = 0; i < bfsNum[t-1]; i++){ int x = bfsList[t-1][i].x, y = bfsList[t-1][i].y; for(int k = 0; k < 7; k++){ int X = x+trans[x%2][k][0], Y = y+trans[x%2][k][1]; if(bricks[to][X][Y] == -1 && !used[X][Y]){ if(X == N){ minTime = t; ky = 1; goto done; } used[X][Y] = 1; bfsList[t][bfsNum[t]].x = X, bfsList[t][bfsNum[t]].y = Y; bfsNum[t]++; } } } } done: if(ky) printf("%d\n", minTime+1); else printf("impossible\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator