Mine Number(搜索,暴力) ACM省赛第三届 G
Mine Number
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:
......
..***.
..***.
..***.
......
提示
来源
饭稀
...........
*..*.*..*.*
*.*****.*.*
*.*****.*.*
*..***..*.*
*...*...***
...........
. . . . . . . . . . .
* . . * . * . . * . *
* . * * * * * . * . *
* . * * * * * . * . *
* . . * * * . . * . *
* . . . * . . . * * *
. . . . . . . . . . .
懒得吐槽了,讲一下这道题吧,就是类似扫雷的规则,相信大家都比较熟悉扫雷,只是题中矩阵的数字表示上下左右和中(就是数字所在位置)五个位置中雷的个数。给出要求输入数字矩阵输出唯一确定的符合数字矩阵的图形矩阵'*'表示雷,'.'表示安全。
看似题目要求从数字矩阵推出图形矩阵,我们亦可以认为是要收缩符合已知数字矩阵的图形矩阵,矩阵的每个点只有两种状态(. or *),用搜索实现起来便不是很难。
注意:输入数字数组时要用字符形式存入字符数组中。
货不多说,下面看代码
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char mapp[][];
char num[][];
int n,m,dx[]={,-,,},
dy[]={,,,-};
bool ispos;
bool isout(int x,int y)//判断是否出界
{
if( x<||x>=m)
return ;
if(y<||y>=n)
return ;
return ;
}
void print(void)//输出字符数组
{
int i,j;
for(i=; i<m; ++i)
{
for(j=; j<n; ++j)
printf("%c ",mapp[i][j]);
printf("\n");
}
}
/*void printn(void)
{
int i,j;
for(i=0; i<m; ++i)
{
for(j=0; j<n; ++j)
printf("%c ",num[i][j]);
printf("\n");
}
}*/
int prints()//判断最后一行是否全为零
{
int j;
for(j=; j<n; ++j)
if(num[m-][j]!='')
return ;
return ;
}
void dfs(int x,int y)
{
// cout<<x<<' '<<y<<endl;
// cout<<"--------------"<<endl;
// printn();
// print();
if(ispos)
return;
if(x==m&&y==)
{
if(prints())
{
ispos=true;
print();
}
return;
}
int xx,yy,k,falg=;
for(k=;k<=;++k)//判断四周是否有0,
{
xx=x+dx[k];
yy=y+dy[k];
if( isout(xx,yy) && num[xx][yy]=='' )
{
falg=;//标记周围有0,不能不为'*'
break;
}
} if(falg)//没有标记,则可以放地雷
{
if(isout(x-,y)&&num[x-][y]!='')//若上方不唯1,则前面填错,需回溯
return;
mapp[x][y]='*';
for(k=;k<=;++k)//若放地雷,周围标记地雷数-1
{
xx=x+dx[k];
yy=y+dy[k];
if( isout(xx,yy))
num[xx][yy]--;
}
if(y==n-)
dfs(x+,);
else
dfs(x,y+);
for(k=;k<=;++k)//周围标记地雷数+1恢复
{
xx=x+dx[k];
yy=y+dy[k];
if(isout(xx,yy))
num[xx][yy]++;
}
}
mapp[x][y]='.';
if(isout(x-,y)&&num[x-][y]!='')//若上方不唯0,则前面填错,需回溯
return;
if(y==n-&&!ispos)
dfs(x+,);
else if(!ispos)
dfs(x,y+);
}
int main()
{
int i,test,t_num;
cin>>test;
for(t_num=;t_num<=test;++t_num)
{
cin>>m>>n;
for(i=;i<m;++i)
scanf("%s",num[i]);
printf("Case %d:\n",t_num);
ispos=;
dfs(,);
}
return ;
}
Mine Number(搜索,暴力) ACM省赛第三届 G的更多相关文章
- [河南省ACM省赛-第三届] 房间安排 (nyoj 168)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=168 分析:找到一天中需要最多的房间即可 #include<iostream> ...
- [河南省ACM省赛-第三届] AMAZING AUCTION (nyoj 251)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=251 规则: 1.若某竞标价唯一,则胜出 2.若不存在唯一竞标价,则投标次数最少竞标价中标 ...
- [河南省ACM省赛-第三届] 网络的可靠性 (nyoj 170)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170 根据题意,需要找到度数为1的结点个数,如下图: #include<iostre ...
- [河南省ACM省赛-第三届] 聪明的kk (nyoj 171)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=171 动态规划: d(i,j) = max{d(i-1, j), d(i, j-1)}+m ...
- [河南省ACM省赛-第三届] BUYING FEED (nyoj 248)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...
- [河南省ACM省赛-第三届] 素数 (nyoj 169)
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #i ...
- [2012山东省第三届ACM大学生程序设计竞赛]——Mine Number
Mine Number 题目:http://acm.sdut.edu.cn/sdutoj/problem.php? action=showproblem&problemid=2410 Time ...
- 山东省第三届ACM省赛
Solved ID PID Title Accepted Submit A 2407 Impasse (+) 0 0 B 2415 Chess 0 0 C 2414 An interest ...
- 第九届蓝桥杯国赛+第二天的第11届acm省赛的总结
第九届蓝桥杯国赛+第二天的第11届acm省赛的总结 25号坐的去北京的火车,10个小时的火车,然后挤了快两个小时的地铁,最终达到了中国矿业大学旁边的订的房间.12个小时很难受,晕车症状有点严重,吃了快 ...
随机推荐
- FW: Chef集中管理工具实践之 (1) 环境部署
本文转载:http://heylinux.com/archives/2208.html Chef集中管理工具实践之 (1) 环境部署 目录结构Chef集中管理工具实践之 (0) 什么是ChefChef ...
- LoadRunner 多场景批处理
@echo off echo *********************************echo ****多场景测试*************echo ******************** ...
- 删除sde用户问题
删除SDE用户(GIS地图数据用户),长时间删除没反应,结束drop user sde cascade命令后,重新执行,结果报ORA-00604 ORA-21700 select user_id,us ...
- TextView 中添加超链接
在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址,一种是设置autolink,让系统自动识别超链接,下面为大家介绍下这两种方法的实现 代码如下: 第一种 pu ...
- 利用jquery实现自动登录
前提是需要引入jquery和jquery.cookie html 用户名:<input type="text" id="name"/><br ...
- CentOS 6.5下搭建LAMP环境详细步骤
1.确认搭建LAMP所需的环境是否已经安装: [root@localhost ~]#rpm -q make gcc gcc-c++ zlib-devel libtool libtool-ltdl li ...
- python模块使用案例
python模块使用案例 一.使用MySQLdb模块代码示例: # 导入 MySQLdb模块 import MySQLdb # 和服务器建立链接,host是服务器ip,我的MySQL数据库搭建在本机, ...
- springMVC + Spring + MyBatis 整合
整理下SSM(基于注解)的整合 1. web.xml 配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...
- DNS解析过程
参考: http://www.maixj.net/ict/dns-chaxun-9208 http://blog.it985.com/8389.html DNS(Domain Name System) ...