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. VR全景智慧城市-720全景项目行业应用

    VR虚拟现实.VR全景概念已成为科技发展热议的焦点.在这样的市场大环境下,全景智慧城市做为一家对大众创新万众创业和用户体验为理念的VR全景城市化信息搜素平台平地而生成为的VR行业领跑者,致力VR全景V ...

  2. Windows Server 2003服务器无法下载.exe文件的解决方法

    今天架设了一台Windows Server 2003的网站服务器,发现打开网页后无法下载网站中的.exe文件,经过研究问题得以解决,拿来做个备忘. 解决方法非常简单,只需要在IIS中,将网站属性里的执 ...

  3. appframework(jqmobi) 3.0 设置

    $(document).on("panelunload",'#mainPage',function(e){ alert('dddddd'); }); 1.重写 data-load ...

  4. js 分享到按钮

    基础的思路,可以在此基础加强 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  5. Android IOS WebRTC 音视频开发总结(七五)-- WebRTC视频通信中的错误恢复机制

    本文主要介绍WebRTC视频通信中的错误恢复机制(我们翻译和整理的,译者:jiangpeng),最早发表在[这里] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID:blac ...

  6. rediscluster 集群操作(摘抄)

    一:关于redis cluster 1:redis cluster的现状 目前redis支持的cluster特性 1):节点自动发现 2):slave->master 选举,集群容错 3):Ho ...

  7. redis基础使用

    redis分linux,window两个版本分支. redis在window下的使用先下载相关包.下载地址:https://github.com/MSOpenTech/redis/releases 下 ...

  8. 【转载】H264--2--语法及结构

    名词解释 场和帧 :    视频的一场或一帧可用来产生一个编码图像.在电视中,为减少大面积闪烁现象,把一帧分成两个隔行的场. 片:             每个图象中,若干宏块被排列成片的形式.片分为 ...

  9. HDOJ(1728)逃离迷宫

    HDOJ 1728 http://acm.hdu.edu.cn/showproblem.php?pid=1728 BFS求最少转过的弯 #include <stdio.h> #includ ...

  10. JNI Local Reference Changes in ICS

    [This post is by Elliott Hughes, a Software Engineer on the Dalvik team. — Tim Bray] If you don’t wr ...