搜索之广搜

P1162 填涂颜色

在方阵外围加一圈零,方便搜索

bfs代码:

#include<bits/stdc++.h>
using namespace std;
int mapp[32][32] = {0}, n;
bool visit[32][32] = {0};
int dis_x[4] = {0, 0, 1, -1};
int dis_y[4] = {1, -1, 0, 0};
struct Node{
	int x; 
	int y;
};
 
void bfs() {
	queue<Node> que;
 	Node start;
 	start.x = 0; start.y = 0;
 	que.push(start);
 	visit[0][0] = 1;
 	while(!que.empty()) {
 		Node now = que.front();
 		que.pop();
 		for(int i = 0; i < 4; i++) {
 			int t_x = now.x + dis_x[i];
 			int t_y = now.y + dis_y[i];
 			if(mapp[t_x][t_y] == 0 && !visit[t_x][t_y] && t_x >= 0 && t_x <= n + 1 && t_y >= 0 && t_y <= n + 1) {
			 	visit[t_x][t_y] = 1;
			 	mapp[t_x][t_y] = 2;
			 	Node neww;
			 	neww.x = t_x;
			 	neww.y = t_y;
				que.push(neww);
			}
		}
	}
	return ;
}
 		
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> mapp[i][j];
		}
	}
	bfs();
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			if(mapp[i][j] == 2) cout << 0 << ' ';
			else if(mapp[i][j] == 1) cout << 1 << ' ';
			else cout << 2 << ' ';
		}
		cout << endl;
	}
	return 0;
}

dfs代码:

#include<bits/stdc++.h>
using namespace std;
int node[32][32] = {0}, n;
bool visit[32][32] = {0};
int dis_x[4] = {0, 0, 1, -1};
int dis_y[4] = {1, -1, 0, 0};
 
void dfs(int x, int y) {
	if(x < 0 || x > n + 1 || y < 0 || y > n + 1 || node[x][y] || visit[x][y]) return ;
	node[x][y] = 2;
	visit[x][y] = 1;
	for(int i = 0; i < 4; i++) {
		int t_x = x + dis_x[i];
		int t_y = y + dis_y[i];
		dfs(t_x, t_y);
	}
}		

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> node[i][j];
		}
	}
	dfs(0, 0);
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			if(node[i][j] == 2) cout << 0 << ' ';
			else if(node[i][j] == 0) cout << 2 << ' ';
			else cout << 1 << ' ';
		}
		cout << endl;
	}
	return 0;
}

P1443 马的遍历

#include<bits/stdc++.h>
using namespace std;
int n, m, x, y;
int mapp[405][405];
int dir_x[] = {1, 1, -1, -1, 2, 2, -2, -2};
int dir_y[] = {2, -2, 2, -2, 1, -1, 1, -1};
bool visit[405][405] = {0};

struct node{
	int x;
	int y;
	int step;
};

void bfs() {
	node start;
	start.x = x;
	start.y = y;
	start.step = 0;
	mapp[start.x][start.y] = 0;
	visit[start.x][start.y] = 1;
	queue<node> q;
	q.push(start);
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		int t_step = now.step;
		for(int i = 0; i < 8; i++) {
			int t_x = now.x + dir_x[i];
			int t_y = now.y + dir_y[i];
			if(t_x < 1 || t_x > n || t_y < 1 || t_y > m || visit[t_x][t_y] )
				continue;
			node newn;
			newn.x = t_x;
			newn.y = t_y;
			newn.step = t_step + 1;
			visit[newn.x][newn.y] = 1;
			mapp[newn.x][newn.y] = newn.step;
			q.push(newn);
		}
	}
	return ;
}
			
 
int main() {
	ios::sync_with_stdio(false);
//	cin >> n >> m >> x >> y;
	scanf("%d %d %d %d", &n, &m, &x, &y);
	memset(mapp, -1, sizeof(mapp));
	bfs();
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
//			cout << mapp[i][j] << ' ';
			printf("%-5d", mapp[i][j]);
		}
		printf("\n");
	}
	return 0;
}

P1032 字串变换

(手动标星)

这题用到了pair 和set容器

  • pair取代了结构体
  • set用于存储字符串的各个形态,去重
#include<bits/stdc++.h>
using namespace std;
string str1, str2;
string from[7], to[7];
int num;//规则条数
queue<pair<string, int>> q;
set<string> s;//去重 

int bfs() {
	int cnt;
	q.push(make_pair(str1, 0));
	string now, next;
	while(!q.empty()) {
		cnt = (q.front()).second;
		if(cnt >= 10) return -1;//变换步数不超过十,等于10的情况下文判断了 
		now = (q.front()).first;
		q.pop();
		for(int i = 0; i < num; i++) {
			int pos = now.find(from[i]);
			while(-1 != pos) {
				next = now;
				next.replace(pos, from[i].length(), to[i]);
				if(0 == s.count(next)) {
					if(next == str2) return cnt + 1;
					s.insert(next);
					q.push(make_pair(next, cnt + 1));
				}
				pos = now.find(from[i], pos + 1);//!!now字符串中可能包含多个from[i] 
			}
		}
	}
	return -1;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> str1 >> str2;
	int i = 0;
	char str[20];
	while(cin >> from[i] >> to[i]) 	i++;
	num = i;
	if(str1 == str2) {
		printf("0\n");
		return 0;
	}
	int cnt = bfs();
	if(-1 == cnt) printf("NO ANSWER!\n");
	else printf("%d\n",cnt);
	return 0;
}
/*
救命,我才知道这个输入最后ctrl + Z 结束 
make_pair在c++11前后修改了声明,有两种格式:
	1、make_pair(str, pos)
	2、make_pair<string, int>(string(str), int(pos))
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值