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. zhuang 定制iOS 7中的导航栏和状态栏

    近期,跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这也就意味着导航栏 ...

  2. oracle中的查询语句(关于出库入库信息表,明细表,把捆包箱表,单位信息表的集中查询)

    --查出所有现金中心的单位IDwith AllUnitas(select t.ORGANIZATIONID orgid,t.parentidfrom CDMS_ORGANIZATION t where ...

  3. Python自动化 【第五篇】:Python基础-常用模块

    目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...

  4. PHP 程序员的技术成长规划

    [导读] 按照了解的很多PHP LNMP程序员的发展轨迹,结合个人经验体会,抽象出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段PHP程序员的技术要求,来帮助很多PHP程序 ...

  5. Solr Cloud搭建

    1:搭建tomcat 配置connector: server.xm文件中: <Connector port="8080"maxThreads="200" ...

  6. SSO(单点登录)

    自己动手写SSO(单点登录) 标签: sso 登录 | 发表时间:2014-03-02 15:01 | 作者: 分享到: 出处:http://www.iteye.com SSO在我们的应用中非常常见, ...

  7. Brn系列网上商城数据库说明文档

    单店版BrnShop_1.9.351数据字典:点击下载 多店版BrnMall_1.9.496数据字典:点击下载 有对网上商城程序设计感兴趣的朋友,欢迎加入QQ群:235274151,大家可以交流下!

  8. Spring RMI (Spring 远程方法调用)

    所需jar包...?    不纠结,一股脑儿全导! 源码地址:http://pan.baidu.com/s/1jG8eOmy 先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成j ...

  9. Intellij_idea-14官方快捷键中文版

    编辑类: Ctrl+Space 基本代码实例(类.方法.变量) Ctrl + Shift + Space 智能代码实例(根据需要的类型过滤方法和变量) Ctrl + Shift + Enter 完整的 ...

  10. SQL Server Profiler使用方法

    一.SQL Server Profiler使用方法 1.单击开始--程序--Microsoft SQL Server 2005--性能工具--SQL Server Profiler,如下图:   2. ...