Gym 100971A Treasure Island
来源:互联网 发布:哪个走路软件可以赚钱 编辑:程序博客网 时间:2024/05/18 01:42
题目链接
题目字面意思是让?变成 . 或#,使得 . 可以连成一片,唯一就把图输出,不唯一就输出Ambiguous,否则Impossible.
先将所有的 . dfs遍历一遍,记录有几块相连的陆地数k,并记录扫过的格子数cnt_s,途中遇到的?就把它当成 . ,如果有没遇到的?说明被#包围,则这个?一定为#。遍历完后,如果k>1,那么肯定是Imossible,如果k为1,再把dfs过程中遇到的每一个?当成#,分别在dfs,计算相连的陆地数cnt,如果有cnt==cnt_s-1,那么有多解,否则解唯一。
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#define MAX_N 55using namespace std;char map[MAX_N][MAX_N];int n,m;int cnt_s;int cnt_p;int cnt;int dx[4]={1,0,-1,0};int dy[4]={0,1,0,-1};bool vis[MAX_N][MAX_N];void dfs(int sx,int sy){ vis[sx][sy]=true; for(int i=0;i<4;i++) { int tx=sx+dx[i]; int ty=sy+dy[i]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]!='#'&&!vis[tx][ty]) { cnt++; if(map[tx][ty]=='?') map[tx][ty]='*';//标记走过的? vis[tx][ty]=true; dfs(tx,ty); } } return ;}int main(void){ while(scanf("%d%d",&n,&m)==2) { cnt_p=0; int sx,sy; for(int i=0;i<n;i++) scanf("%s",map[i]); memset(vis,false,sizeof(vis)); int k=0;//k块陆地 for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(map[i][j]=='.'&&!vis[i][j]) { k++; cnt=1; dfs(i,j); sx=i; sy=j; } } } if(k>1) { printf("Impossible\n"); continue; } cnt_s=cnt; bool ok=false; for(int i=0;i<n&&!ok;i++) { for(int j=0;j<m&&!ok;j++) { cnt=1; if(map[i][j]=='*') { memset(vis,false,sizeof(vis)); map[i][j]='#'; dfs(sx,sy); if(cnt==cnt_s-1) ok=true; map[i][j]='.'; } } } if(ok) printf("Ambiguous\n"); else { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(map[i][j]=='.') printf("."`; else printf("#"); } printf("\n"); } } } return 0;}
0 0
- Gym 100971A Treasure Island
- GYM 100971 A.Treasure Island(dfs+并查集)
- GYM Samara16 A Treasure Island 暴力
- (二分,BFS).Treasure Island
- Codeforces 106 D Treasure Island
- cf 106d Treasure Island
- HDU 2416 Treasure of the Chimp Island
- Treasure of the Chimp Island hdu bfs
- POJ 3346 Treasure of theChimp Island(BFS)
- Treasure of the Chimp Island (BFS
- CFgym:Treasure Island(搜索 & 思维)
- A LONELY ISLAND
- 634A.Island Puzzle
- CodeForces 494A Treasure
- codeforces 494A Treasure
- CodeForces 494A Treasure
- HDU 2416 Treasure of the Chimp Island bfs 最短路
- hdu(2416) Treasure of the Chimp Island
- 欢迎使用CSDN-markdown编辑器
- Google 2016 面试题2 | 不构造树的情况下验证先序遍历
- SpringBoot整合Quartz 2实现定时任务之一:整合
- HDU 5778(abs 暴力)
- 剑指offer54--二叉树的多行打印
- Gym 100971A Treasure Island
- MySQL information_schema说明
- 17-07-31 android 方法 如何引用链接获取json数据(改进版)
- Python——9函数式编程②
- request 生命周期及参数信息
- 51nod 1133 不重叠的线段(贪心)
- 信号与槽机制
- sublime text安装及package control的安装
- 借助 Lucene.Net 构建站内搜索引擎(上)