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

我晕,数据王记初始化搞死循环了,贡献一次TLE

Posted by KatrineYang at 2016-11-16 13:03:10 on Problem 1551 and last updated at 2016-11-16 13:53:41
#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:
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