1:迷宫问题
定义一个二维数组:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
struct Node {
int x, y;
Node(int xx, int yy) : x(xx), y(yy) {};
};
int dir[4][2] = { {0,1} ,{0,-1}, {1,0}, {-1,0} };
bool color[5][5];
int pre[26];
void Print(int n) {
if (n == -1)
return;
Print(pre[n]);
printf("(%d, %d)\n", n/5,n%5);
}
void bfs(bool map[][5]) {
fill(pre, pre + 25,-1);
memset(color, 0, sizeof(color));
color[0][0] = 1;
queue<Node>q;
q.push(Node(0, 0));
while (!q.empty()) {
Node node = q.front();
if (node.x == 4 && node.y == 4) {
Print(24);
break;
}
for (int i = 0; i < 4; i++) {
int x = node.x + dir[i][0];
int y = node.y + dir[i][1];
if (x >= 0 && x < 5 && y >= 0 && y < 5) {
if (!color[x][y] && map[x][y]==0) {
color[x][y] = 1;
pre[x * 5 + y] = node.x * 5 + node.y;
q.push(Node(x, y));
}
}
}
q.pop();
}
}
int main() {
bool map[5][5];
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
cin >> map[i][j];
bfs(map);
return 0;
}
2:Pots
-
总时间限制:
- 1000ms 内存限制:
- 65536kB
-
描述
-
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
输入
-
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
输出
-
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
int a, b, c;
struct Node {
int x, y;
int step; /* 记录步数 */
Node(int xx, int yy,int steps) : x(xx), y(yy),step(steps) {};
};
struct PreNode {
int val = 0;
int oper = 0;
};
bool color[101][101];
PreNode pre[1000001];
void Print(int n) {
if (n == 0)
return;
Print(pre[n].val);
switch (pre[n].oper) {
case 1:cout << "DROP(1)" << endl; break;
case 2:cout << "DROP(2)" << endl; break;
case 3:cout << "FILL(1)" << endl; break;
case 4:cout << "FILL(2)" << endl; break;
case 5:cout << "POUR(1,2)" << endl; break;
case 6:cout << "POUR(2,1)" << endl; break;
default:cout << "?" << endl; break;
}
}
void bfs() {
bool flag = 0;
memset(color, 0, sizeof(color));
color[0][0] = 1;
queue<Node>q;
q.push(Node(0, 0, 0));
while (!q.empty()) {
Node node = q.front();
if (node.x == c || node.y == c) {
flag = 1;
cout << node.step << endl;
Print(node.x * 100 + node.y);
break;
}
if (!color[0][node.y]) { /* drop(1)*/
color[0][node.y] = 1;
q.push(Node(0, node.y, node.step+1));
pre[node.y].val = node.x*100+node.y;
pre[node.y].oper = 1;
}
if (!color[node.x][0]) { /* drop(2) */
color[node.x][0] = 1;
q.push(Node(node.x, 0, node.step+1));
pre[node.x*100].val = node.x * 100 + node.y;
pre[node.x * 100].oper = 2;
}
if (!color[a][node.y]) { /* fill(1) */
color[a][node.y] = 1;
q.push(Node(a, node.y, node.step+1));
pre[a * 100 + node.y].val = node.x * 100 + node.y;
pre[a * 100 + node.y].oper = 3;
}
if (!color[node.x][b]) { /* fill(2) */
color[node.x][b] = 1;
q.push(Node(node.x, b, node.step + 1));
pre[node.x * 100 + b].val = node.x * 100 + node.y;
pre[node.x * 100 + b].oper = 4;
}
if (node.x + node.y <= b) { /* a能全倒进b里面 */
if (!color[0][node.x+node.y]) { /* pour(1,2)*/
color[0][node.x + node.y] = 1;
q.push(Node(0,node.x+node.y,node.step+1));
pre[node.x + node.y].val = node.x * 100 + node.y;
pre[node.x + node.y].oper = 5;
}
}
else {
if (!color[node.x+node.y-b][b]) { /* pour(1,2)*/
color[node.x + node.y - b][b] = 1;
q.push(Node(node.x + node.y - b, b, node.step + 1));
pre[(node.x +node.y-b)*100+b].val = node.x * 100 + node.y;
pre[(node.x + node.y - b) * 100 + b].oper = 5;
}
}
if (node.x + node.y <= a) { /* b能全倒进a里面 */
if (!color[node.x + node.y][0]) { /* pour(2,1)*/
color[node.x + node.y][0] = 1;
q.push(Node(node.x + node.y, 0, node.step + 1));
pre[(node.x +node.y)*100].val = node.x * 100 + node.y;
pre[(node.x + node.y) * 100].oper = 6;
}
}
else {
if (!color[a][node.x + node.y - a]) { /* pour(2,1)*/
color[a][node.x + node.y - a] = 1;
q.push(Node(a, node.x + node.y - a, node.step + 1));
pre[a*100 + node.x+node.y - a].val = node.x * 100 + node.y;
pre[a * 100 + node.x + node.y - a].oper = 6;
}
}
q.pop();
}
if (!flag)
cout << "impossible" << endl;
}
int main() {
cin >> a >> b >> c;
bfs();
return 0;
}
3:鸣人和佐助
-
总时间限制:
- 1000ms 内存限制:
- 65536kB
-
描述
-
佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?

已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?
输入
-
输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
输出
- 输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。
#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
int a, b, c;
struct Node {
int m, n,t;
int step;
Node(int xx, int yy,int tt,int steps) : m(xx), n(yy),t(tt),step(steps) {};
};
char map[201][201];
bool color[201][201][11];
int dir[4][2] = { {0,-1},{0,1},{-1,0},{1,0} };
int startX, startY;
int endX, endY;
bool flag = 0;
void bfs() {
memset(color, 0, sizeof(color));
queue<Node>q;
q.push(Node(startX,startY,c,0));
while (!q.empty()) {
Node node = q.front();
if (node.m == endX&&node.n == endY) {
cout << node.step << endl;
flag = 1;
break;
}
for (int i = 0; i < 4; i++) {
int x = node.m + dir[i][0];
int y = node.n + dir[i][1];
int t = node.t;
if (x>=0&&x<a&&y>=0&&y<b) {
if (!color[x][y][t]) {
if (map[x][y] == '*'||map[x][y]=='+'&&t >= 0) {
q.push(Node(x, y, t, node.step + 1));
color[x][y][t] = 1;
}
if (map[x][y] == '#'&&t >= 1) {
q.push(Node(x, y, t - 1, node.step + 1));
color[x][y][t - 1] = 1;
}
}
}
}
q.pop();
}
if (!flag)
cout << "-1" << endl;
}
int main() {
cin >> a >> b >> c;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++) {
cin >> map[i][j];
if (map[i][j] == '@') {
startX = i;
startY = j;
}
if (map[i][j] == '+') {
endX = i;
endY = j;
}
}
bfs();
return 0;
}
2842

被折叠的 条评论
为什么被折叠?



