A - Treasure Island

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

standard input/output 
Announcement

 
  • Statements

    Pirate John Silver has found a map depicting exactly one island in a sea. The map is a piece of cloth divided into cells: n cells in height and m cells in width. John Silver knows that every cell denotes either land or water, but some of the cells are erased, and now it's absolutely impossible to say what these cells represent.

    Help John Silver to restore the map of the island. An island is a non-empty set of land cells connected in four directions (up, down, left and right).

Input

The first line contains two integers n and m(1 ≤ n,  m ≤ 50) — the sizes of the map.

Each of the next n lines contains m characters and describes the map. A character is equal to «#» if this cell is a water cell, «.» if it's a land cell, and «?» if this cell is erased.

It's guaranteed that the input contains at least one character «.» and at least one character «?».

Output

If it's not possible to restore the map so that it would depict exactly one island, output «Impossible».

If the map can be restored in a unique way, output n lines of m characters in the same format as they are in the input, but replacing «?» with «.» or «#».

And if there are several correct ways to restore the map, output «Ambiguous».

Sample Input

Input
5 7

#######

#..#..#

#..?..#

#..#..#

#######
Output
#######

#..#..#

#.....#

#..#..#

#######
Input
5 7

#######

#...#.#

#.?.?.#

#.#...#

#######
Output
Ambiguous
Input
5 7

#######

#.#.#.#

#.#?#.#

#.#.#.#

#######
Output
Impossible

题意:给你一个地图,'#'代表水,'.'代表陆地,'?'代表擦去的地图,可能是'#'也可能是'.'。地图中本该只有一块相连的陆地,若只有一种方案则输出确定的地图。若有多种方案,则输出‘Ambiguous’,若无答案,则输出‘Impossible’。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; #define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f;
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue; char sc[55][55];
int a[55][55],n,m,s,vis[55][55]; int dx[]={-1,0,0,1,-1};
int dy[]={-1,1,-1,0,0}; void dfs(int x,int y)
{
vis[x][y]=1;
s++;
FOR(i,4) {
int xx=x+dx[i],yy=y+dy[i];
if(a[xx][yy]&&!vis[xx][yy]) dfs(xx,yy);
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
MM(a,0);MM(vis,0);
int x,y,cnt=0;
FOR(i,n) {
scanf("%s",sc[i]+1);
FOR(j,m) if(sc[i][j]=='.') {a[i][j]=1;x=i;y=j;}
else if(sc[i][j]=='?') a[i][j]=2;
}
FOR(i,n) FOR(j,m)
if(a[i][j]==1&&!vis[i][j]){
s=0;cnt++;
dfs(i,j);
}
if(cnt>1) {printf("Impossible\n");CT;}
int s0=s,ok=0;
for(int i=1;i<=n&&!ok;i++) for(int j=1;j<=m&&!ok;j++)
if(a[i][j]==2){
MM(vis,0);
a[i][j]=0;
s=0;
dfs(x,y);
if(s==s0) ;
else if(s==s0-1) ok=1;
else a[i][j]=1;
}
if(ok) {printf("Ambiguous\n");CT;}
FOR(i,n) {
FOR(j,m)
if(a[i][j]) printf(".");
else printf("#");
printf("\n");
}
}
return 0;
}

思维错误点:一直想着怎么用并查集去连接大陆,结果还要特殊处理下,?与周围.和#的 各种排列

关系,复杂多了;

 

分析:这道题在思维上有难度,看了题解,有好几个值得学习的地方;

1. 将图之外的区域和不能访问的#设置为0,这样就避免了额外判断边界。

2.判断一个?是否是连接陆地所必要的,采用控制变量法,先假设其他的?都是陆地,这块陆地是

海洋那么从某个已经确定的陆地搜一遍,如果得到的陆地的个数跟原来的一样,那么这个?肯定是在

大陆不能访问到的大海中,肯定设为0,如果比原来陆地个数小1,那么这块?区域对于连接大陆不是必要的

那么ambiguous了,如果不是以上两种情况,则显然是必要的,那么设置为1,然后再去判断其他的?;

最新文章

  1. Dash文档制作教程
  2. SVD java 算法实现
  3. (1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长 (2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。 该类包含有成员变量: radius:public 修饰的double类型radius,表示圆的半径。 x:private修饰的double型变量x,
  4. BIEE定制化
  5. 完整成功配置wamp server小记
  6. JS 清除字符串数组中,重复元素
  7. Line Search and Quasi-Newton Methods
  8. JS 人民币大写
  9. C语言格式化输出,空位补0,空位补空格
  10. zabbix 布署实践【2 agent安装】
  11. linux(centos)下安装PHP的PDO扩展
  12. CUDA5.5 的环境变量设置
  13. [认证授权] 5.OIDC(OpenId Connect)身份认证(扩展部分)
  14. Linux 下 boost 库的安装,配置个人环境变量
  15. JavaWeb:servlet实现下载与上传功能
  16. sql 把一个用逗号分隔的多个数据字符串变成一个表的一列
  17. Navicat for MySQL 64位破解版
  18. JS字典 Dictionary类
  19. Html、Asp、Php、Jsp禁止页面缓存
  20. 【BZOJ】【3673】可持久化并查集 &amp; 【3674】可持久化并查集加强版

热门文章

  1. 【hash】珍珠
  2. ssh无密登录_集群分发脚本xsync
  3. 怎样理解String的slice(), subString(), substr()三个方法
  4. 修改mac系统的host文件 (一)
  5. java——多线程知识点大总结
  6. EasyUI_DataGrid数据操作
  7. Eclipse错误提示: Symbol &#39;xxxx&#39; could not be resolved
  8. 【题解】JSOI2008 最大数
  9. vue学习(2)-过滤器
  10. ioping测试