【Gym 100971A】Treasure Island
题意
题目链接
给你一个地图,'#'代表水,'.'代表陆地,'?'代表擦去的地图,可能是'#'也可能是'.'。地图中本该只有一块相连的陆地,若只有一种方案则输出确定的地图。若有多种方案,则输出‘Ambiguous’,若无答案,则输出‘Impossible’。
分析
将所有‘.’进行dfs扫一遍,dfs时遇到的‘?’当作'.',因为被'#'包围的'?'一定代表‘#’。
一边记录相连的陆地数量b,一边记录当前相连陆地的总格数s0。
如果b==1,就对每个遇到过的'?',将其置为'#‘,再dfs一遍,如果扫到总格数小于s0-1,则有多种答案,否则就是一种答案。
如果b>1,则是无答案。
代码
#include <bits/stdc++.h>
#define N 55
int n, m, a[N][N], u[N][N], x, y, b, s0, s, ok, fx[] = {, , , -}, fy[] = {, -, , };
char c;
void dfs(int x, int y)
{
if (u[x][y]) return;
u[x][y] = ;
s++;
if (a[x][y] == ) a[x][y] = ;
for (int i = ; i < ; i++)
{
int nx = x + fx[i];
int ny = y + fy[i];
if (a[nx][ny])
dfs(nx, ny);
}
}
int main()
{
scanf("%d%d ", & n, & m);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
c = getchar();
if (c == '.') a[i][j] = ;
else if (c == '?') a[i][j] = ;
}
getchar();
}
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
if (a[i][j] == && !u[i][j])
{
b++;
dfs(i, j);
x = i;
y = j;
}
if(b == )
{
s0 = s;
for (int i = ; i <= n && !ok; i++)
for (int j = ; j <= m && !ok; j++)
if (a[i][j] == )
{
memset(u, , sizeof u);
s = a[i][j] = ;//? => #
dfs(x, y);
if(s == s0 - )
ok = ;
a[i][j] = ;
}
if (ok)
printf("Ambiguous");
else
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
if (a[i][j] == ) printf(".");
else printf("#");
printf("\n");
}
}
else printf("Impossible");
return ;
}
【Gym 100971A】Treasure Island的更多相关文章
- 【Codeforces 494A】Treasure
[链接] 我是链接,点我呀:) [题意] 让你把"#"用至少一个右括号代替 使得整个括号序列合法 [题解] 首先我们不要考虑井号 考虑最简单的括号序列 并且把左括号看成1,右括号看 ...
- 【Codeforces 979B】Treasure Hunt
[链接] 我是链接,点我呀:) [题意] 每次你可以将一个字符变成一个不同于本身的字符. 每个人需要改变n次(且不能不改变) 设每个人的字符串中出现次数最多的字符出现的次数为cnt[0~2] 问你谁的 ...
- 【 Gym 101116K 】Mixing Bowls(dfs)
BUPT2017 wintertraining(15) #4H Gym - 101116K 题意 给定一个菜谱,大写的单词代表混合物,小写的代表基础原料.每个混合物由其它混合物或基础原料组成,不会间接 ...
- 【 Gym - 101124E 】Dance Party (数学)
BUPT2017 wintertraining(15) #4G Gym - 101124 E.Dance Party 题意 有c种颜色,每个颜色最多分配给两个人,有M个男士,F个女士,求至少一对男士同 ...
- 【Gym - 101124A】The Baguette Master (数学,几何)
BUPT2017 wintertraining(15) #4F Gym - 101124A 题意 给定画框宽度,画的四边和一个对角线长度,求画框外沿周长. 题解 过顶点做画框的垂线,每个角都得到两个全 ...
- 【 Gym - 101138K 】 The World of Trains (DP)
BUPT2017 wintertraining(15) #4E Gym - 101138K 题意 N节车厢的火车,每节车厢容量是1~K,那么有\(K^N\)种火车. 求选择D个连续的且容量相同的车厢的 ...
- 【 Gym - 101138J 】Valentina and the Gift Tree(树链剖分)
BUPT2017 wintertraining(15) 4 D Gym - 101138J 数据 题意 n个节点的一棵树,每个节点的权值为g,q个询问,树上的节点U-V,求U到V的路径的最大子段和. ...
- 【 Gym - 101138F 】GukiZ Height (数学)
BUPT2017 wintertraining(15) #4 C Gym - 101138F 题意 初始高度0,目标值h,第i天目标值会下降i,当前高度会改变a[i%n],求高度不小于目标值的最早的时 ...
- 【 Gym - 101138D 】Strange Queries (莫队算法)
BUPT2017 wintertraining(15) #4B Gym - 101138D 题意 a数组大小为n.(1 ≤ n ≤ 50 000) (1 ≤ q ≤ 50 000)(1 ≤ ai ≤ ...
随机推荐
- Zookeeper C API 指南一(转)
Zookeeper 监视(Watches) 简介 Zookeeper C API 的声明和描述在 include/zookeeper.h 中可以找到,另外大部分的 Zookeeper C API 常量 ...
- Debian下安装deb格式安装包
dpkg -i 软件包名称 就好啦 下面是相应链接: http://blog.csdn.net/lhf_tiger/article/details/7493400
- Codeforces Round #274 Div.1 C Riding in a Lift --DP
题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(安装ubuntu虚拟机)-(5)
之前介绍了在webvirtmgr平台下创建centos,windows server 2008的虚拟机,今天说下创建ubuntu虚拟机的过程. (1)首先下载ubuntu16.04的iso镜像放到/u ...
- html5中上传图片
从相册中选择图片上传 function uploadFromAlbum(type) { var dirtype = ""; if ("pick_store_license ...
- 使用DataAnnotations实现数据验证
https://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx http://www.cnblo ...
- 微软职位内部推荐-Senior Development Engineer
微软近期Open的职位: Job Title: Senior Software Development Engineering Work Location: Suzhou, China Enterpr ...
- C#实现对指定文件夹中文件按修改时间排序
string path = "~/Document/Introduction/团队管理制度/"; DirectoryInfo dirinfo = new Di ...
- Java系列:JVM指令详解(上)(zz)
一.未归类系列A 此系列暂未归类. 指令码 助记符 说明 59:iastore 60:lload 6 //因为str ...
- [CareerCup] 10.4 Find All Duplicates Elements 寻找所有的重复项
10.4 You have an array with all the numbers from 1 to N, where N is at most 32,000. The array may ha ...