Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

1A。暴力,0ms

Posted by KatrineYang at 2016-08-30 13:21:22 on Problem 1233
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator