M - A計劃
解題思路
- 從起點出發進行bfs,把可以走的位置入隊
- 但碰到了傳送陣的話,需要判斷是不是牆和傳送陣,因為這兩種情況都會讓騎士找不到公主
- 剩下的就是正常bfs的過程
程式碼實現
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include <limits.h>
#define u unsigned
#define ll long long
#define sc scanf
#define pr printf
#define fr(i, j, n) for (int i = j; i < n; i++)
#define N 10
typedef struct {
int x;
int y;
int z;
int s;
}p;
int d[4][3] = {
{0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}
};
int t;
int n;
int m;
int c;
char s[2][N][N];
int bfs();
int min1(int a, int b) {
return a <= b ? a : b;
}
int main(int argc, char* argv[])
{
sc("%d", &c);
while (c--) {
sc("%d%d%d", &n, &m, &t);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
sc("%s", s[i][j]);
}
}
if (bfs() <= t) {
pr("YES\n");
}
else {
pr("NO\n");
}
}
return 0;
}
int bfs()
{
p q[N * N * 2] = { 0 };
bool v[2][N][N] = { 0 };
p t = { 0 };
int ans = INT_MAX;
int l = 0;
int r = 0;
p temp = { 0 };
q[r++] = t;
v[0][0][0] = 1;
while (l < r) {
t = q[l++];
if (s[t.x][t.y][t.z] == '#')
{
t.x = !t.x;
if (s[t.x][t.y][t.z] == '*') {
continue;
}
if (s[t.x][t.y][t.z] == '#') {//坑點1:和洛谷的那個題目不一樣,這個傳送過去可能是#,那麼就一直橫跳
continue;
}
}
if (s[t.x][t.y][t.z] == 'P') {//傳送的地方還可能是終點所以也要判斷
ans = min1(ans, t.s);
continue;
}
for (int i = 0; i < 4; i++) {
int nx = t.x + d[i][0];
int ny = t.y + d[i][1];
int nz = t.z + d[i][2];
if (nx >= 0 && nx <= 1 && ny >= 0 && ny < n && nz >= 0 && nz < m && s[nx][ny][nz] != '*' && !v[nx][ny][nz]) {
temp.x = nx;
temp.y = ny;
temp.z = nz;
temp.s = t.s + 1;
v[nx][ny][nz] = 1;
q[r++] = temp;
}
}
}
return ans;
}
Pots
解題思路
- 因為是求最短序列,所以用bfs來搜尋答案
- bfs來遍歷6中情況,因為有兩個瓶子,一共三種操作
程式碼實現
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include <limits.h>
#define u unsigned
#define ll long long
#define sc scanf
#define pr printf
#define fr(i, j, n) for (int i = j; i < n; i++)
#define N 101
typedef struct {
int a;
int b;
int path[200];
int size;
}p;
int a;
int b;
int c;
bool is;
bool v[N][N][7];
int ans = INT_MAX;
p ans1 = { 0 };
p q[N * N * 6] = { 0 };
int min1(int a, int b) {
return a <= b ? a : b;
}
void bfs()
{
p t = { 0 };
p temp = { 0 };
int l = 0;
int r = 0;
q[r++] = t;
while (l < r) {
t = q[l++];
if (t.a == c || t.b == c) {
pr("%d\n", t.size);
for (int i = 0; i < t.size; i++) {
switch (t.path[i]) {
case 1: {
pr("FILL(1)\n");
break;
}
case 2:
{
pr("FILL(2)\n");
break;
}
case 3: {
pr("DROP(1)\n");
break;
}
case 4:
{
pr("DROP(2)\n");
break;
}
case 5:
{
pr("POUR(1,2)\n");
break;
}
case 6: {
pr("POUR(2,1)\n");
break;
}
}
}
is = 1;
break;
}
for (int i = 1; i <= 6; i++) {
temp = { 0 };
switch (i) {
case 1: {
if (t.a != a && !v[t.a][t.b][i]) {
v[t.a][t.b][i] = 1;
temp = t;
temp.a = a;
temp.path[temp.size] = 1;
temp.size++;
q[r++] = temp;
}
break;
}
case 2: {
if (t.b != b && !v[t.a][t.b][i]) {
v[t.a][t.b][i] = 1;
temp = t;
temp.b = b;
temp.path[temp.size] = 2;
temp.size++;
q[r++] = temp;
}
break;
}
case 3: {
if (t.a != 0 && !v[t.a][t.b][i]) {
v[t.a][t.b][i] = 1;
temp = t;
temp.a = 0;
temp.path[temp.size] = 3;
temp.size++;
q[r++] = temp;
}
break;
}
case 4: {
if (t.b != 0 && !v[t.a][t.b][i]) {
v[t.a][t.b][i] = 1;
temp = t;
temp.b = 0;
temp.path[temp.size] = 4;
temp.size++;
q[r++] = temp;
}
break;
}
case 5: {
if (t.b != b && t.a != 0 && !v[t.a][t.b][i]) {
v[t.a][t.b][i] = 1;
temp = t;
if (temp.b + temp.a >= b) {
temp.a -= b - temp.b;
temp.b = b;
}
else {
temp.b += temp.a;
temp.a = 0;
}
temp.path[temp.size] = 5;
temp.size++;
q[r++] = temp;
}
break;
}
case 6: {
if (t.a != a && t.b != 0 && !v[t.a][t.b][i]) {
v[t.a][t.b][i] = 1;
temp = t;
if (temp.b + temp.a >= a) {
temp.b -= a - temp.a;
temp.a = a;
}
else {
temp.a += temp.b;
temp.b = 0;
}
temp.path[temp.size] = 6;
temp.size++;
q[r++] = temp;
}
break;
}
}
}
}
if (!is) {
pr("impossible\n");
}
}
int main(int argc, char* argv[])
{
sc("%d%d%d", &a, &b, &c);
bfs();
return 0;
}