[2012山东省第三届ACM大学生程序设计竞赛]——Mine Number
Mine Number
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?
action=showproblem&problemid=2410
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描写叙述
Every one once played the game called Mine Sweeping, here I change the rule. You are given an n*m map, every element is a '*' representing a mine, or a '.' representing any other thing. If I ask you what's the total number of mines around (i, j), you should
check (i, j)'s up, down, left, right and also itself (if overstep the boundary, ignore it), if that position is a '*', you should add one to the total number of (i, j), and here I call the number Mine Number. For example, if the map is "..**.. ", we can get
the Mine Number as "012210" easily, but here is the question, if I give you the Mine Number, can you tell me the original map?
输入
The input consists of multiple test cases.
The first line contains a number T, representing the number of test cases.
Then T lines follow. For each case, the first line contains two numbers n and m (1<=n, m<=20).representing the lines and rows. Then following n lines, each line contain m numbers each number represents the Mine Number.
输出
For each case, please print the case number (beginning with 1) and the original map which you reverted. The data guarantee there is only one result.
演示样例输入
2
7 11
10010100101
21223221222
32354532323
32355532323
31235321333
21022201333
10001000111
5 6
001110
013431
014541
013431
001110
演示样例输出
Case 1:
...........
*..*.*..*.*
*.*****.*.*
*.*****.*.*
*..***..*.*
*...*...***
...........
Case 2:
......
..***.
..***.
..***.
......
来源
2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛
一道搜索题,比赛时就是研究这道题,和高恒一起。话说当时方向对了,解题思路非常正确。可是编程时候出了点问题。尤其是最后的代码输出,输出一个字符和一个空格。。
。事实上应该没有空格的。。
。o(╯□╰)o啊。。。
言归正传:
这道题题意非常easy,扫雷玩过吧?
上面的数字3,代表该点八个方向上有三颗雷。
这道题题意也差点儿相同,仅仅是数字所代表的是 上下左右与该点 五个方向的雷数量。
题目会给出数字,让你确定 雷的分布图。每一个数据仅仅输出一个解。
DFS,深度优先搜索。
大体的思路方向是:
从0,0開始往后推断。每一个点是否放雷。根据就是周围的数字(上下左右)是否有0的情况。有0就不放雷。
放雷后就要将五个方向的数字减1,然后继续往后推断。
这是基本的推断,可是显然须要大的前提来 剪掉大半棵树。
→首先将第一行枚举,然后每一行依据上一行状态来做:
假设上一行同位置数字为0,则该点不放雷。
假设上一行同位置数字为1。则该点必须放雷,此时推断四周是否有0的情况,没有则放雷,有则回溯。
假设上一行同位置数字不为0或者1。则回溯。
推断到最后一行,须要将最后一行数组推断,是否全为0,是则输出结果。不是则回溯。
就是这样。
#include <iostream>
#include <string.h>
using namespace std;
#define MAX 25
int n,m,num[MAX][MAX],dis[5][2]={0,0,1,0,-1,0,0,1,0,-1};
char Map[MAX][MAX];
bool ispos;
// 推断出界
bool isout(int x,int y)
{
if( x<0 || y<0 || x>=n || y>=m ) return 1;
return 0;
}
// 推断五个点是否有小于等于0的位置
bool location(int x,int y)
{
int i,xx,yy;
for(i=0;i<5;++i)
{
xx=x+dis[i][0];
yy=y+dis[i][1];
if( !isout(xx,yy) && num[xx][yy]<=0 ) return false;
}
return true;
}
// 假设放雷,五个点数字减1
void change(int x,int y)
{
int i,xx,yy;
for(i=0;i<5;++i)
{
xx=x+dis[i][0];
yy=y+dis[i][1];
if( isout(xx,yy) ) continue;
--num[xx][yy];
}
}
// 假设该地原来放雷,可是应该不放。回溯,五个点数字加1
void c_back(int x,int y)
{
int i,xx,yy;
for(i=0;i<5;++i)
{
xx=x+dis[i][0];
yy=y+dis[i][1];
if( isout(xx,yy) ) continue;
++num[xx][yy];
}
}
// 推断最后一行是否符合条件
bool judge_final(void)
{
int i;
for(i=0;i<m;++i)
if( num[n-1][i]!=0 ) return false;
return true;
}
// 输出结果
void print(void)
{
int i,j;
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
cout<<Map[i][j];
cout<<endl;
}
}
void dfs(int x,int y)
{
if( ispos ) return;
if( x==n )
{
if( judge_final() )
{ispos=1;print();}
return;
}
if( y==m ) {dfs(x+1,0);return;}
if( x==0 ) // 首先第一行要进行枚举。不冲突就可以
{
if( location(x,y) )
{
Map[x][y]='*';
change(x,y);
dfs(x,y+1);
c_back(x,y);
}
Map[x][y]='.';
dfs(x,y+1);
}
else// 其余行,依据上一行对应位置来推断假设做
{
if( num[x-1][y]==0 ) // 上一行为0,此行不能放雷
{
Map[x][y]='.';
dfs(x,y+1);
}
else if( num[x-1][y]==1 ) // 上一行为1,此行必须放雷,推断四周是否有0情况
if( location(x,y) )
{
Map[x][y]='*';
change(x,y);
dfs(x,y+1);
c_back(x,y);
}
}
} int main()
{
int i,j,test,t_num;
char c;
cin>>test;
for(t_num=1;t_num<=test;++t_num)
{
cin>>n>>m;
for(i=0;i<n;++i)
for(j=0;j<m;++j)
{
cin>>c;
num[i][j]=c-'0';
}
cout<<"Case "<<t_num<<":"<<endl;
ispos=0;
dfs(0,0);
}
return 0;
}
[2012山东省第三届ACM大学生程序设计竞赛]——Mine Number的更多相关文章
- [2012山东省第三届ACM大学生程序设计竞赛]——n a^o7 !
n a^o7 ! 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2413 Time Lim ...
- 2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛--n a^o7 ! 分类: 比赛 2015-06-09 17:16 14人阅读 评论(0) 收藏
n a^o7 ! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 All brave and intelligent fighte ...
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
- sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛
Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...
- 2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server
点击打开链接 2226: Contest Print Server Time Limit: 1 Sec Memory Limit: 128 MB Submit: 53 Solved: 18 [Su ...
- 山东省第四届ACM大学生程序设计竞赛解题报告(部分)
2013年"浪潮杯"山东省第四届ACM大学生程序设计竞赛排名:http://acm.upc.edu.cn/ranklist/ 一.第J题坑爹大水题,模拟一下就行了 J:Contes ...
- angry_birds_again_and_again(2014年山东省第五届ACM大学生程序设计竞赛A题)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2877 题目描述 The problems ca ...
- 2013年山东省第四届ACM大学生程序设计竞赛 Alice and Bob
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very ...
- 2013年山东省第四届ACM大学生程序设计竞赛J题:Contest Print Server
题目描述 In ACM/ICPC on-site contests ,3 students share 1 computer,so you can print your source code ...
随机推荐
- python 之 MRO 异常
今天突然遇到这个异常,先贴两个地址,待有时间写博客 https://www.jianshu.com/p/fea6e0a0cc14 https://makina-corpus.com/blog/meti ...
- NodeJS学习笔记 (9)网络服务-https(ok)
模块概览 这个模块的重要性,基本不用强调了.在网络安全问题日益严峻的今天,网站采用HTTPS是个必然的趋势. 在nodejs中,提供了 https 这个模块来完成 HTTPS 相关功能.从官方文档来看 ...
- iOS开发——AFNetworking基于https的使用
应公司项目需求,之前的项目使用的http,新项目要求使用https,这篇博客是在AFNetworking框架基于http的基础上修改而来. 1.在开始前,先要把 .crt 文件转成 .cer 文件,然 ...
- 浅谈python 中正则的一些函数
主要的函数有 : match() search() findall() group() groups() split() match (): 含义 开头匹配,匹配成功返回一个对象失败则 ...
- c++PrimerChap7类
仅仅记录贴,按书上的做完了一边,想把private分离出来已经很难了.因为is用到的成员变量都是直接当做public使用的,如果要改的话可以考虑存储输入,让后用构造函数对类进行初始化. #includ ...
- caioj 1161 欧拉函数3:可见点数
(x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...
- 题解 P3834 【【模板】可持久化线段树 1(主席树)】
可持久化线段树的前置知识是权值线段树,但是你不学也没有太大的关系因为思想不是很难理解. 可持久化线段树支持历史记录查询,这是它赖以解题的方法. 在本题中思路是建立n颗线段树,然后对于每次询问,考虑其中 ...
- 【BZOJ 1150】[CTSC2007]数据备份Backup
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 选择的连接肯定是相邻的点对. 那么我们处理出来长度为n-1的数组a 其中a[i-1] = dis[i]-dis[i-1] 那么问题就 ...
- MarkDown写作之嵌入LaTeX和HTML
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/49788741 Markdown 是一种 ...
- Codeforces 558E 线段树处理字符串内排序
给出长度为n的字符串,m个操作. 每一个操作有三个值 l,r,op. op==1,表示将字符串中[ l ,r ]的部分依照升序排列. op==0,表示将字符串中[ l ,r ]的部分依照降序排列. 输 ...