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 |
我晕,数据王记初始化搞死循环了,贡献一次TLE#include <stdio.h> #include <iostream> using namespace std; int r,c; const int COVERED = 0; const int MINED = 1; const int FLAGGED = 3; const int UNCOVERED = 2; int board[20][20]; int dist[20][20]; int dir[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; int Covered; bool inRange(int x, int y){ return x>0 && x<=r && y>0 && y<=c; } void getDist(){ for(int i = 1; i <= r; i++){ for(int j = 1; j <= c; j++){ if(board[i][j] == MINED) continue; dist[i][j] = 0; for(int k = 0; k < 8; k++){ if(!inRange(i+dir[k][0], j+dir[k][1])) continue; if(board[i+dir[k][0]][j+dir[k][1]] == MINED) dist[i][j]++; } } } } int solve(int x, int y){ //cout << x << " " << y << endl; if(board[x][y] == MINED) return r*c+1; int Board[20][20]; for(int i = 1; i <= r; i++){ for(int j = 1; j <= c; j++){ Board[i][j] = board[i][j]; } } Board[x][y] = UNCOVERED; int covered = Covered-1; //int cnt = 0; while(covered > 0){ //cnt++; bool stuck = 1; for(int i = 1; i <= r; i++){ for(int j = 1; j <= c; j++){ if(Board[i][j] != UNCOVERED) continue; int F = 0, C = 0, M = dist[i][j]; for(int k = 0; k < 8; k++){ if(!inRange(i+dir[k][0], j+dir[k][1])) continue; int st = Board[i+dir[k][0]][j+dir[k][1]]; if(st == FLAGGED) F++; if(st == COVERED || st == MINED) C++; } if(F==M && C>0){ for(int k = 0; k < 8; k++){ if(!inRange(i+dir[k][0], j+dir[k][1])) continue; int st = Board[i+dir[k][0]][j+dir[k][1]]; if(st == COVERED) {Board[i+dir[k][0]][j+dir[k][1]] = UNCOVERED; } } covered -= C; stuck = 0; goto oneRoundDone; } else if(F+C==M && C>0){ for(int k = 0; k < 8; k++){ if(!inRange(i+dir[k][0], j+dir[k][1])) continue; int st = Board[i+dir[k][0]][j+dir[k][1]]; if(st == MINED) {Board[i+dir[k][0]][j+dir[k][1]] = FLAGGED; } } //covered -= C; stuck = 0; goto oneRoundDone; } } } oneRoundDone: if(stuck) break; } return covered; } int main(){ while(1){ scanf("%d%d",&r,&c); if(!r) break; Covered = 0; for(int i = 1; i <= r; i++){ char h[40]; scanf("%s", h); for(int j = 1; j <= c; j++){ if(h[j-1] == '.') { board[i][j] = COVERED; Covered++; } else board[i][j] = MINED; } } getDist(); int mn = r*c+1; for(int i = 1; i <= r; i++){ for(int j = 1; j <= c; j++){ int res = solve(i,j); if(res < mn) mn = res; } } printf("%d\n",mn); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator