Gym 100971A Treasure Island BFS 思维题
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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
5 7 ####### #..#..# #..?..# #..#..# #######
####### #..#..# #.....# #..#..# #######
5 7 ####### #...#.# #.?.?.# #.#...# #######
Ambiguous
5 7 ####### #.#.#.# #.#?#.# #.#.#.# #######
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 思维题的更多相关文章
- Gym - 101572D Distinctive Character bfs 思维
题目传送门 题目大意: 给出n个01串,让你构造一个字符串,使这个字符串和这些字符串中相似程度最高 尽可能低.如果两个字符串对应位置相同,则相似程度加一. 思路: 每一个01串更改自己的一部分后,都可 ...
- D. Treasure Hunting ( 思维题 , 贪心)
传送门 题意: 在一个 n * m 的地图里,有 k 个宝藏,你的起点在 (1, 1), 每次你能 向下向右向左移动(只要在地图里): 现在,有 q 个安全的列, 你只有在这些列上面,你才能 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- 思维题 Gym 100553A Alter Board
题目传送门 /* 题意:一个n×m的矩形,相邻的颜色不同,黑或白.问最少的翻转次数,每次翻转可指定任意一个子矩形 思维题:最少要把偶数行和列翻转,也就是n/2+m/2次 */ #include < ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- hdu5325 树的思维题
pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...
- D. Treasure Island
D. Treasure Island dfs大法好== 写半天bfs疯狂MLE dfs标记掉路上的一些点 然后再跑一遍dfs #include<bits/stdc++.h> using n ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
随机推荐
- Redis-数据结构与对象-对象
1. 作用 Redis使用对象作为五种不同类型的底层实现,字符串,列表,哈希,集合,有序集合等 而对象是基于之前的分析的数据结构创建的.每个对象都至少用到一种数据结构,这意味着,Redis五大类型,底 ...
- 【多重背包】Transport Ship
[来源] 2018年焦作网络赛 [参考博客] https://blog.csdn.net/baymax520/article/details/82719454 [题意] 有N种船只,每种船只的载货量为 ...
- Web前端开发CSS基础
CSS 层叠样式表(英文全称:Cascading Style Sheets),是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.CSS不 ...
- 并不对劲的bzoj4538:loj2049:p3250:[HNOI2016]网络
题意 有一棵\(n\)(\(n\leq 10^5\))个点的树,\(m\)(\(m\leq 2\times 10^5\))个操作.操作有三种:1.给出\(u,v,k\),表示加入一条从\(u\)到\( ...
- 添加Entity Framework Core,数据库迁移
1.建立Entity 建立IEntity的接口 建立实现IEntity接口的抽象类Entity 建立类继承抽象类Entity 2. 数据库放到infrastructure的项目中 3.注册和配置这个d ...
- git 查看当前仓库地址以及设置新的仓库地址
1.查看当前仓库地址 git remote show origin 2.设置新的仓库地址 1.先登录 gitlab 查看当前仓库地址: 执行修改地址命令 git remote set-url orig ...
- 深入Java虚拟机之内存区域与内存溢出
一.内存区域 Java虚拟机在执行Java程序的过程中会把他所管理的内存划分为若干个不同的数据区域.Java虚拟机规范将JVM所管理的内存分为以下几个运行时数据区:程序计数器.Java虚拟机栈.本地方 ...
- js 的一些小技巧
来源:https://www.w3cplus.com/javascript/javascript-tips.html 1.确保数组的长度 在处理网格结构时,如果原始数据每行的长度不相等,就需要重新创建 ...
- Flutter-动画-概念篇
一.Flutter中的动画的基本概念图 二.Flutter各动画的概念 视图动画 补间动画 就是一个View,定义了起点和终点.时间以及运动曲线,并按照所定规则由起点运动到终点的过程. 帧动画 帧动画 ...
- Java学习笔记【一、环境搭建】
今天把java的学习重新拾起来,一方面是因为公司的项目需要用到大数据方面的东西,需要用java做语言 另一方面是原先使用的C#公司也在慢慢替换为java,为了以后路宽一些吧,技多不压身 此次的学习目标 ...