NOI Judge2.5-1818 红与黑

写的这个看着确实有点不想深度。。。

本质上其实就是走过的方格划掉然后递推就可以解决

不需要回溯

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 21 //最大值为20,加一防止RE 
using namespace std;
int x,y; //注意: x为列数,y为行数
int start_x,start_y; //开始时的坐标,输入里不会提供具体值 
int sum = 0; //计数器,初始不为一因为后面会变符号 
char a[maxn][maxn];
void _show(){
	cout <<"-------------------------------------"<<endl;
	for(int i = 1; i<= y; i++){
		for(int j = 1; j <= x; j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	cout <<"-------------------------------------"<<endl;
}
void dfs(int x, int y){
//	cout << y << x << endl;
	a[y][x] = '$'; //$符号表示是黑色但是走过了
	if(a[y][x+1] == '.'){
//		_show();
//		cout << y << x << endl;
		dfs(x+1,y);//递归 
	}
	if(a[y][x-1] == '.'){
//		cout << y << x << endl;
//		_show();
		dfs(x-1,y);//递归 
	}
	if(a[y+1][x] == '.'){
//		cout << y << x << endl;
//		_show();
		dfs(x,y+1);//递归 
	}
	if(a[y-1][x] == '.'){
//		cout << y << x << endl;
//		_show();
		dfs(x,y-1);//递归 
	}
	
}
int main()
{
		cin >> x >> y;
		if(x == 0 && y == 0){
			return 0;
		}
		for(int i = 0; i <= y; i++){
			for(int j = 0; j <= x; j++){
				a[i][j] = '*';//初始化,输入完之后*表示边界(其实是我不喜欢判断的时候加上一行判断语句 ) 
	 		}
		}
		for(int i = 1;i <= y; i++){
			for(int j =1; j <= x; j++){
				cin >> a[i][j];
				if(a[i][j] == '@'){
					start_y = i;
					start_x = j; //记录初始坐标值 
				}
			}
		}
		dfs(start_x,start_y);
		for(int i = 1; i<= y; i++){
			for(int j = 1; j <= x; j++){
				if(a[i][j] == '$'){
					sum++;
				}
			}
		}
		cout << sum << endl; //打印计数器 
    return 0;
}

 

这是我最开始的代码

过样例so easy

但是WA了

然后我终于发现了一个问题

多个数据集合

果断上循环!

然后就有了这个:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 21 //最大值为20,加一防止RE 
using namespace std;
int x,y; //注意: x为列数,y为行数
int start_x,start_y; //开始时的坐标,输入里不会提供具体值 
int sum = 0; //计数器,初始不为一因为后面会变符号 
char a[maxn][maxn];
void _show(){
	cout <<"-------------------------------------"<<endl;
	for(int i = 1; i<= y; i++){
		for(int j = 1; j <= x; j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	cout <<"-------------------------------------"<<endl;
}
void dfs(int x, int y){
//	cout << y << x << endl;
	a[y][x] = '$'; //$符号表示是黑色但是走过了
	if(a[y][x+1] == '.'){
//		_show();
//		cout << y << x << endl;
		dfs(x+1,y);//递归 
	}
	if(a[y][x-1] == '.'){
//		cout << y << x << endl;
//		_show();
		dfs(x-1,y);//递归 
	}
	if(a[y+1][x] == '.'){
//		cout << y << x << endl;
//		_show();
		dfs(x,y+1);//递归 
	}
	if(a[y-1][x] == '.'){
//		cout << y << x << endl;
//		_show();
		dfs(x,y-1);//递归 
	}
	
}
int main()
{
	while(1){
		cin >> x >> y;
		if(x == 0 && y == 0){
			return 0;
		}
		for(int i = 0; i <= y; i++){
			for(int j = 0; j <= x; j++){
				a[i][j] = '*';//初始化,输入完之后*表示边界(其实是我不喜欢判断的时候加上一行判断语句 ) 
	 		}
		}
		for(int i = 1;i <= y; i++){
			for(int j =1; j <= x; j++){
				cin >> a[i][j];
				if(a[i][j] == '@'){
					start_y = i;
					start_x = j; //记录初始坐标值 
				}
			}
		}
		dfs(start_x,start_y);
		for(int i = 1; i<= y; i++){
			for(int j = 1; j <= x; j++){
				if(a[i][j] == '$'){
					sum++;
				}
			}
		}
		cout << sum << endl; //打印计数器 
		sum = 0; //sum清空 
	}
	
    return 0;
}

你以为这就AC了吗?

错!

还是WA

最终在洛谷询问后知道了原因

我初始化不对

应该把给的行数列数各加一才能达到在地图外围加边界的情况

AC代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 50 //最大值为20,加一防止RE 
using namespace std;
int x,y; //注意: x为列数,y为行数
int start_x,start_y; //开始时的坐标,输入里不会提供具体值 
int sum = 0; //计数器,初始不为一因为后面会变符号 
char a[maxn][maxn];
void dfs(int x, int y){
//	cout << y << ',' << x << endl;
	a[y][x] = '$'; //$符号表示是黑色但是走过了
	if(a[y][x+1] == '.'){
//		_show();
//		cout << y << ',' << x << endl;
		dfs(x+1,y);//递归 
	}
	if(a[y][x-1] == '.'){
//		cout << y << ',' << x << endl;
//		_show();
		dfs(x-1,y);//递归 
	}
	if(a[y+1][x] == '.'){
//		cout << y << ',' << x << endl;
//		_show();
		dfs(x,y+1);//递归 
	}
	if(a[y-1][x] == '.'){
//		cout << y << ',' << x << endl;
//		_show();
		dfs(x,y-1);//递归 
	}
	return;
}
int main()
{
	while(1){
		cin >> x >> y;
		if(x == 0 && y == 0){
			break;
			return 0;
		}
		for(int i = 0; i <= y+1; i++){
			for(int j = 0; j <= x+1; j++){
				a[i][j] = '*';//初始化,输入完之后*表示边界(其实是我不喜欢判断的时候加上一行判断语句 ) 
	 		}
		}
		for(int i = 1;i <= y; i++){
			for(int j =1; j <= x; j++){
				cin >> a[i][j];
				if(a[i][j] == '@'){
					start_y = i;
					start_x = j; //记录初始坐标值 
//					cout << "初始坐标为:" << start_y << ',' << start_x << endl;
				}
			}
		}
		dfs(start_x,start_y);
		for(int i = 1; i<= y; i++){
			for(int j = 1; j <= x; j++){
				if(a[i][j] == '$'){
					sum++;
				}
			}
		}
		cout << sum << endl; //打印计数器 
		sum = 0; //sum清空 
	}
    return 0;
}

不知道用的是不是dfs反正随便起的函数名

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值