山东省第7届ACM省赛 E题

D - The Binding of Isaac

Time Limit: 2000 ms / Memory Limit: 65536 kb

Description

Ok, now I will introduce this game to you...

Isaac is trapped in a maze which has many common rooms…

Like this…There are 9 common rooms on the map.

And there is only one super-secret room. We can’t see it on the map. The super-secret room always has many special items in it. Isaac wants to find it but he doesn’t know where it is.Bob 
tells him that the super-secret room is located in an empty place which is adjacent to only one common rooms. 
Two rooms are called adjacent only if they share an edge. But there will be many possible places.

Now Isaac wants you to help him to find how many places may be the super-secret room.

Input

Multiple test cases. The first line contains an integer T (T<=3000), indicating the number of test case.
Each test case begins with a line containing two integers N and M (N<=100, M<=100) indicating the number
of rows and columns. N lines follow, “#” represent a common room. “.” represent an empty place.Common rooms 
maybe not connect. Don’t worry, Isaac can teleport.

Output

 One line per case. The number of places which may be the super-secret room.

Sample Input
2
5 3
..#
.##
##.
.##
##.
1 1
#
Sample Output

8

4


题意大概是若每个点旁边只要有一个#,就算符合题意的点,求点的个数。注意所有的输入示例的外层可能都存在着点。所以输入的时候提前将周边的点输入。


然后利用函数查找,遍历四个方向,若只找到一个#,返回1,其他的返回0.暴力即可解决。



#include<stdio.h>
#include<string.h>


int next[4][2]={0,1,1,0,0,-1,-1,0};  //方向数组 
int m,n;
 
char map[105][105];      //储存地图 


int find(int x,int y)   //查找函数 
{
int tx,ty,k,count;  //count用来记录,tx,ty接收下一个 方向 

count=0;
for(k=0;k<4;k++)
{

tx=next[k][0]+x;
ty=next[k][1]+y;

if(tx<0||tx>n+1||ty<0||ty>m+1)   //越界判断 
continue;

if(map[tx][ty]=='#')    //找到# 
{
count++;
}

if(count>=2)    //大于2 直接返回 
return 0;
}
if(count==1)
return 1;

return 0;
}


int main()
{
int t,i,j,sum;

scanf("%d",&t);

while(t--)
{
memset(map,0,sizeof(map));   //清空map,貌似没用 
scanf("%d%d",&n,&m);

getchar();    //吸收回车 
sum=0;

for(j=1;j<=m;j++)   //外围加上点 
{
map[0][j]='.';
map[n+1][j]='.'; 
}
for(i=1;i<=n;i++)  //外围加点,但四个角不要 
{
map[i][0]='.';
map[i][m+1]='.';
}
for(i=1;i<=n;i++)   //输入 
{
for(j=1;j<=m;j++)
scanf("%c",&map[i][j]);

getchar();
}
for(i=0;i<=n+1;i++)    //遍历 
for(j=0;j<=m+1;j++)
{
if(map[i][j]=='.') 
sum+=find(i,j);
}

printf("%d\n",sum);

}
return 0;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值