toj2194Mine(dfs)
来源:互联网 发布:淘宝大图轮播尺寸 编辑:程序博客网 时间:2024/06/07 00:10
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 101 51 72 32 54 85 27 77 98 69 21 59 101 51 72 32 54 85 27 77 98 69 25 50 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;}
- toj2194Mine(dfs)
- DFS
- DFS
- dfs
- dfs
- dfs
- dfs
- DFS
- DFS
- dfs
- DFS
- DFS
- DFS
- dfs
- DFS
- dfs
- dfs
- dfs
- 在Windows Azure上基于CentOS 6.3部署MySQL 5.6主从式数据库(1)
- 二叉树的最近父结点的查找(Least Common Ancestor)
- Linux基础.控制台式查看日志
- LeetCode | String to Integer (atoi)
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
- toj2194Mine(dfs)
- 算法导论 5.1-3
- java提高篇(十四)-----关键字final
- Linux 下压缩与解压.zip和.rar及.7z文件
- 变换不变性
- Html5初学_____(2)
- 消息队列、AMQP和RabbitMQ
- ubuntu系统下关于环境变量的修改
- x264中I,P,B帧和PTS,DTS的关系