Homework Introduction

#include <bits/stdc++.h>
using namespace std;
const int N = 205;
int n, m, book[N][N], sx, sy, fx, fy;
char mat[N][N];

int nxt[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};

struct node {
	int x, y, cost;
	friend bool operator < (node a, node b) {
		return a.cost > b.cost;
	}
};

void bfs() {
	memset(book, 0x3f, sizeof(book));
	book[sx][sy] = 0;
	priority_queue<node>q;
	q.push({sx, sy, 0});
	while (!q.empty()) {
		node tmp = q.top();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = tmp.x + nxt[i][0];
			int ny = tmp.y + nxt[i][1];
			if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
				if (mat[nx][ny] != '#') {
					int cost = tmp.cost + 1;
					if (mat[nx][ny] == 'G')
						cost++;
					if (cost < book[nx][ny]) {
						book[nx][ny] = cost;
						q.push({nx, ny, cost});
					}
				}
			}
		}
	}
	if (book[fx][fy] == 0x3f3f3f3f)
		cout << "You can't save Mengxin" << endl;
	else
		cout << book[fx][fy] << endl;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == '@')
				sx = i, sy = j;
			if (mat[i][j] == 'M')
				fx = i, fy = j;
		}
	}
	bfs();
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, m, k;
int book[N][N];

struct node {
	int a, b, cost;
	string s;
};

void bfs() {
	queue<node>q;
	q.push({0, 0, 0, ""});
	book[0][0] = 1;
	while (!q.empty()) {
		node tmp = q.front();
		q.pop();
		if (tmp.a == k || tmp.b == k) {
			cout << tmp.cost << endl;
			cout << tmp.s << endl;
			exit(0);
		}
		//1.将1装满
		if (tmp.a != n && book[n][tmp.b] == 0) {
			book[n][tmp.b] = 1;
			q.push({n, tmp.b, tmp.cost + 1, tmp.s + "FILL(1)\n"});
		}
		//2.将2装满
		if (tmp.b != m && book[tmp.a][m] == 0) {
			book[tmp.a][m] = 1;
			q.push({tmp.a, m, tmp.cost + 1, tmp.s + "FILL(2)\n"});
		}
		//3.将1倒空
		if (tmp.a > 0 && book[0][tmp.b] == 0) {
			book[0][tmp.b] = 1;
			q.push({0, tmp.b, tmp.cost + 1, tmp.s + "DROP(1)\n"});
		}
		if (tmp.b > 0 && book[tmp.a][0] == 0) {
			book[tmp.a][0] = 1;
			q.push({tmp.a, 0, tmp.cost + 1, tmp.s + "DROP(2)\n"});
		}
		//将a倒入b
		if (tmp.b + tmp.a <= m && book[0][tmp.a + tmp.b] == 0) {
			q.push({0, tmp.a + tmp.b, tmp.cost + 1, tmp.s + "POUR(1,2)\n"});
		} else if (tmp.b + tmp.a > m && book[tmp.b + tmp.a - m][m] == 0) {
			q.push({tmp.a + tmp.b - m, m, tmp.cost + 1, tmp.s + "POUR(1,2)\n"});
		}
		//
		if (tmp.b + tmp.a <= n && book[tmp.a + tmp.b][0] == 0) {
			q.push({tmp.a + tmp.b, 0, tmp.cost + 1, tmp.s + "POUR(2,1)\n"});
		} else if (tmp.b + tmp.a > n && book[n][tmp.a + tmp.b - n] == 0) {
			q.push({n, tmp.a + tmp.b - n, tmp.cost + 1, tmp.s + "POUR(2,1)\n"});
		}
	}
}

int main() {
	cin >> n >> m >> k;
	bfs();
	cout<<"impossible"<<endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int sx, sy, fx, fy;

int n, m, book[N][N], nxt[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
char mat[N][N];

struct node {
	int x, y, cost;
	friend bool operator <(node a, node b) {
		return a.cost > b.cost;
	}
};

void bfs() {
	priority_queue<node>q;
	q.push({sx, sy, 0});
	memset(book, 0x3f, sizeof(book));
	book[sx][sy] = 0;
	while (!q.empty()) {
		node tmp = q.top();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = tmp.x + nxt[i][0];
			int ny = tmp.y + nxt[i][1];
			if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
				if (mat[nx][ny] != '#') {
					int cost = tmp.cost + 1;
					if (mat[nx][ny] > '0' && mat[nx][ny] <= '9') {
						cost += mat[nx][ny] - '0';
					}
					if (cost < book[nx][ny]) {
						book[nx][ny] = cost;
						q.push({nx, ny, cost});
					}
				}
			}
		}
	}
	if (book[fx][fy] == 0x3f3f3f3f)
		cout << "IMPOSSIBLE" << endl;
	else
		cout << book[fx][fy] << endl;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'Z')
				sx = i, sy = j;
			if (mat[i][j] == 'W')
				fx = i, fy = j;
		}
	}
	bfs();
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
int a, b, book[10005];

struct node {
	int val, cost;
	friend bool operator < (node a, node b) {
		return a.cost > b.cost;
	}
};

int judge(int x) {
	if (x < 2)
		return 0;
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)
			return 0;
	}
	return 1;
}

void bfs() {
	memset(book, 0x3f, sizeof(book));
	priority_queue<node>q;
	q.push({a, 0});
	book[a] = 0;
	while (!q.empty()) {
		node tmp = q.top();
		q.pop();
		//	cout << tmp.val << " " << tmp.cost << endl;
		for (int i = 0; i <= 9; i++) {
			//个位
			int val = tmp.val / 10 * 10 + i;
			if (tmp.cost + 1 < book[val] && judge(val)) {
				book[val] = tmp.cost + 1;
				q.push({val, tmp.cost + 1});
			}
			//十位
			val = tmp.val / 100 * 100 + i * 10 + tmp.val % 10;
			if (tmp.cost + 1 < book[val] && judge(val)) {
				book[val] = tmp.cost + 1;
				q.push({val, tmp.cost + 1});
			}
			val = tmp.val / 1000 * 1000 + i * 100 + tmp.val % 100;
			if (tmp.cost + 1 < book[val] && judge(val)) {
				book[val] = tmp.cost + 1;
				q.push({val, tmp.cost + 1});
			}
			val = i * 1000 + tmp.val % 1000;
			if (tmp.cost + 1 < book[val] && judge(val) && val > 999) {
				book[val] = tmp.cost + 1;
				q.push({val, tmp.cost + 1});
			}
		}
	}
	if (book[b] == 0x3f3f3f3f)
		book[b] = 0;
	cout << book[b] << endl;
}

int main() {
	int T;
	cin >> T;
	while (T--) {
		cin >> a >> b;
		bfs();
	}
	return 0;
}
Status
Done
Problem
9
Open Since
2025-5-8 9:00
Deadline
2025-5-16 23:59
Extension
24 hour(s)