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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值