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:
......
..***.
..***.
..***.
......

提示

 

来源

2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛

饭稀

 赛题重现是遇到此题,比赛是觉得挺麻烦,虽然有了思路,但并没有拿出时间来做。比赛过后,做了一下,代码还是比较容易写的,只是一直WA,自认为明明已经完美的代码就是不给过,最后发现一个让我想撞墙的问题,输出的地图中没有空格,我的输出即使在美观,亦不会AC的。
正确答案是这样的:
...........
*..*.*..*.*
*.*****.*.*
*.*****.*.*
*..***..*.*
*...*...***
...........
 
而我的答案是这样滴:
. . . . . . . . . . .
* . . * . * . . * . *
* . * * * * * . * . *
* . * * * * * . * . *
* . . * * * . . * . *
* . . . * . . . * * *
. . . . . . . . . . .

懒得吐槽了,讲一下这道题吧,就是类似扫雷的规则,相信大家都比较熟悉扫雷,只是题中矩阵的数字表示上下左右和中(就是数字所在位置)五个位置中雷的个数。给出要求输入数字矩阵输出唯一确定的符合数字矩阵的图形矩阵'*'表示雷,'.'表示安全。

看似题目要求从数字矩阵推出图形矩阵,我们亦可以认为是要收缩符合已知数字矩阵的图形矩阵,矩阵的每个点只有两种状态(. 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的更多相关文章

  1. [河南省ACM省赛-第三届] 房间安排 (nyoj 168)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=168 分析:找到一天中需要最多的房间即可 #include<iostream> ...

  2. [河南省ACM省赛-第三届] AMAZING AUCTION (nyoj 251)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=251 规则: 1.若某竞标价唯一,则胜出 2.若不存在唯一竞标价,则投标次数最少竞标价中标 ...

  3. [河南省ACM省赛-第三届] 网络的可靠性 (nyoj 170)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170 根据题意,需要找到度数为1的结点个数,如下图: #include<iostre ...

  4. [河南省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 ...

  5. [河南省ACM省赛-第三届] BUYING FEED (nyoj 248)

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...

  6. [河南省ACM省赛-第三届] 素数 (nyoj 169)

    #include <iostream> #include <cstdio> #include <queue> #include <cstring> #i ...

  7. [2012山东省第三届ACM大学生程序设计竞赛]——Mine Number

    Mine Number 题目:http://acm.sdut.edu.cn/sdutoj/problem.php? action=showproblem&problemid=2410 Time ...

  8. 山东省第三届ACM省赛

    Solved ID PID Title Accepted Submit   A 2407 Impasse (+) 0 0   B 2415 Chess 0 0   C 2414 An interest ...

  9. 第九届蓝桥杯国赛+第二天的第11届acm省赛的总结

    第九届蓝桥杯国赛+第二天的第11届acm省赛的总结 25号坐的去北京的火车,10个小时的火车,然后挤了快两个小时的地铁,最终达到了中国矿业大学旁边的订的房间.12个小时很难受,晕车症状有点严重,吃了快 ...

随机推荐

  1. (转载)关于web端功能测试的测试方向

    一.功能测试 1.1链接测试 链接是web应用系统的一个很重要的特征,主要是用于页面之间切换跳转,指导用户去一些不知道地址的页面的主要手段,链接测试一般关注三点: 1)链接是否按照既定指示那样,确实链 ...

  2. oracle创建包后执行报错:object omgmig.test_package is invalid.

    今天学习了一下oracle的包的写法,然后碰到这么个问题.包声明和包主体都正确,但是就是执行报错:object omgmig.test_package is invalid. 这是会报错的sql,看起 ...

  3. spring security method security

    参考 Spring Security 官方文档 http://www.concretepage.com/spring/spring-security/preauthorize-postauthoriz ...

  4. mininet之miniedit可视化操作

    Mininet 2.2.0之后的版本内置了一个mininet可视化工具miniedit,使用Mininet可视化界面方便了用户自定义拓扑创建,为不熟悉python脚本的使用者创造了更简单的环境,界面直 ...

  5. python 排序

    python 写的排序,实现起来还是比较简单 #快速排序 def qsort(L): if len(L)>1: return qsort([i for i in L[1:] if i<L[ ...

  6. js 模块开发之一(模块开发价值)

    首先引用我们的今天的主角 ----<前端模块化开发的价值> 1,前端开发最常见的两个问题 ---命名冲突和文件依赖 2,对于命名冲突的基本解决办法就是学习其他语言的习惯,添加命名空间 va ...

  7. JQuery 简单的文字超出部分隐藏下拉显示

    HTML代码: <body> <div class="txt_bos"><!--在每一个放置文字的class外面包一个div,以便设置动画样式用,同样 ...

  8. JQuery Object vs. DOM element

    JQuery Object 和 DOM的区别 HTML DOM 定义了访问和操作HTML文档的标准方法.其中 document 是DOM 树的根对象 ,在浏览器宿主环境中,可以通过JS操作HTML D ...

  9. Servlet 利用Session实现不重复登录

    import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.It ...

  10. 算法与数据结构实验题6.4 order (二叉树)

    1.题目: 2.代码: #include<iostream> #include<algorithm> using namespace std; struct Node { in ...