码迷,mamicode.com
首页 > 其他好文 > 详细

Gym 100971A Treasure Island BFS 思维题

时间:2016-07-23 22:57:17      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

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,然后再去判断其他的?;

Gym 100971A Treasure Island BFS 思维题

标签:

原文地址:http://www.cnblogs.com/smilesundream/p/5699572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!