toj2194Mine(dfs)

2194.   Mine
Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 1734   Accepted Runs: 624



You must be very familiar with the game "Minesweeper". It is played on an N*N grid, and there are M mines hided in the grid. Your task is to mark the mines' positions without touching them.

If a position containing a mine is touched, you lose the game. If a position not containing a mine is touched, an integer K (0 ≤ K ≤ 8) appears indicating that there are K mines in the eight adjacent positions. If K = 0, the eight adjacent positions will be touched automatically, new numbers will appear and this process is repeated until no new number is 0.

Given the distribution of the mines, output the numbers appearing after the player's first touch.

Input

The first line of each case is two numbers N (1 ≤ N ≤ 100) and M (0 ≤ M ≤ N*N), indicating the size of the grid and the number of mines. Each of the following M lines contains two numbers Xi and Yi (1 ≤ Xi,Yi ≤ N) indicating there is a mine in the Xi-th row and Yi-th colomn. You can assume there is at most one mine in one position. The last line of each case is two numbers X and Y, indicating the position of the player's first touch.

The input is terminated by a test case starting with N = M = 0. This test case should not be processed.

Output

If the player touches the mine, just output "oops!".

If the player doesn't touch the mine, output the numbers appearing after the touch. If a position is touched by the player or by the computer automatically, output the number. If a position is not touched, output a dot '.'.

Output a blank line after each test case.

Sample Input

9 10
1 5
1 7
2 3
2 5
4 8
5 2
7 7
7 9
8 6
9 2
1 5

9 10
1 5
1 7
2 3
2 5
4 8
5 2
7 7
7 9
8 6
9 2
5 5

0 0

Sample Output

oops!

.........
.........
..12111..
..10001..
..10001..
1110011..
000012...
11101....
..101....

Author: Roba


#include <iostream>
#include <cstring>
using namespace std;
int vis[105][105];
bool mine[105][105];
int dir[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int n,m,x,y,sx,sy,flag;
int getNumOfMine(int x, int y){
    int cnt = 0;
    for(int i=0; i<8; i++){
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if(tx<0 || tx>n-1 || ty<0 || ty>n-1) continue;
        if(mine[tx][ty]) cnt++;
    }
    return cnt? cnt:-1;
}
void dfs(int x, int y){
    if(flag==1) return ;
    if(mine[x][y]) {flag = 1; return;}
    int cnt = getNumOfMine(x,y);//shi 0 jiu search
    if(cnt==-1){//cnt=0;
        vis[x][y]=0;
        for(int i=0; i<8; i++){
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];
            if(tx<0 || tx>n-1 || ty<0 || ty>n-1 || vis[tx][ty]!=-1) continue;
            int cnt_2 = getNumOfMine(tx,ty);
            if(cnt_2!=-1) vis[tx][ty]=cnt_2;
            else dfs(tx,ty);
        }
    }else{
        vis[x][y]=cnt;
        return;
    }
}
int main()
{
    while(cin>>n>>m && n+m){
        memset(vis,-1,sizeof(vis));
        memset(mine,false,sizeof(mine));
        for(int i=0; i<m; i++){
            cin>>x>>y;
            mine[x-1][y-1]=true;
        }
        cin>>sx>>sy;
        dfs(sx-1,sy-1);
        if(!flag){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    if(vis[i][j]!=-1) cout<<vis[i][j];
                    else cout<<".";
                }
                cout<<endl;
            }
        }else cout<<"oops!"<<endl;
        cout<<endl;
        flag = 0;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章