信息学奥赛一本通 1191:流感传染 (耙耙)

http://ybt.ssoier.cn:8088/problem_show.php?pid=1191
在这里插入图片描述
感觉 坑好多。
新建一个b的二维数组,用来存流感的感染次数与关系
比如 一开始时候
00000
00010
00100
00000
00000
第二天
00020
00212
00120
00200
00000
第三天
00320
00212
00123
03230
00300

注意.必须是 ‘.’ 才可以允许感染

if(a[xx][yy]=='.') 	b[xx][yy]=k;

AC代码:

#include<bits/stdc++.h>
using namespace std;
char a[105][105]; 
int  b[105][105];
int n;
int m;
int g[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int ans;
void print(){
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cout<<b[i][j];
		}
		cout<<endl;
	}
	cout<<endl<<endl;
}
int main(){
	freopen("cpp.in","r",stdin);
	freopen("cpp.out","w",stdout);
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>a[i][j];
			if(a[i][j]=='@'){
				b[i][j] = 1 ;
			}
		}
	}
	cin>>m;
	for(int k=2;k<=m;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				//找到病毒后, 将他们上下左右都设置成病毒 
				if(b[i][j]==k-1){
					for(int p=0;p<4;p++){
						int xx = i + g[p][0];
						int yy = j + g[p][1]; 
						if( xx>=0 && yy>=0 && xx<n && yy<n  ){ 
							if(a[xx][yy]=='.')
								b[xx][yy]=k;
						}
					}
				}
			}
		}
//		print();
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(b[i][j]>0){
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值