这个题应该是很简单的一道题,,,但是我先做的
Dungeon Master
POJ - 2251 然后.....两道题又特别像.....然后 就掉进坑里,,..........爬出来了....发现是bfs在入队列判断那儿有问题,,将范围控制在0-L,R,C即可,而不是题目定义的最大整数50.... 但是这个是针对第二版WA的代码,,,第一版的直接是'0'和‘1’的还在坑里面....
3 4 5 20
0 0 0 0 0
0 1 1 1 0
0 1 1 0 0
1 1 1 0 1
1 1 1 1 1
1 1 1 1 1
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 1 1 1 1
1 0 1 1 1
1 1 1 1 0
AC:
#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
int L, R, C, SL, SR, SC, EL, ER, EC, Time;
char t;
char T[2] = {'.', '#'};
char map[53][53][53];
bool pos[53][53][53];
int dir[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
struct Node
{
int l, r, c; //position
int step;
char t;
// bool flag;
Node(){
l = r = c = 0;
step = 0;
t = '.';
// flag = false;
}
Node(int q, int b, int d, int w, char m){
l = q, r = b, c = d, step = w, t = m;
}
};
Node tmp1;
void bfs()
{
queue<Node> q;
// Node tmp(SL, SR, SC, 0, 'S');
Node tmp;
tmp.l = SL;
tmp.r = SR;
tmp.c = SC;
tmp.step = 0;
tmp.t = 'S';
// cout << tmp.t << endl;
q.push(tmp);
pos[SL][SR][SC] = true;
while(!q.empty()){
tmp1 = q.front();
if(tmp1.t == 'E'){
return;
}
q.pop();
for(int i=0; i<6; i++){
Node tmp2;
tmp2.l = tmp1.l+dir[i][0];
tmp2.r = tmp1.r+dir[i][1];
tmp2.c = tmp1.c+dir[i][2];
// cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << " " << map[tmp2.l][tmp2.r][tmp2.c] << endl;
//judge
if((map[tmp2.l][tmp2.r][tmp2.c] == '.'||map[tmp2.l][tmp2.r][tmp2.c] == 'E') && (!pos[tmp2.l][tmp2.r][tmp2.c]) && tmp2.l<=L && tmp2.r<=R && tmp2.c<=C && tmp2.l>0 && tmp2.r>0 && tmp2.c>0){
// tmp2.flag = true;
tmp2.step = tmp1.step+1;
tmp2.t = map[tmp2.l][tmp2.r][tmp2.c];
// cout << tmp2.t << " ";
q.push(tmp2);
pos[tmp2.l][tmp2.r][tmp2.c] = true;
map[tmp2.l][tmp2.r][tmp2.c] = '#';
}
}
// cout << endl;
}
}
int main()
{
int t2;
scanf("%d", &t2);
// cin >> t2;
// memset(map, '#', sizeof(map));
while(t2--){
// cin >> L >> R >> C >> Time;
scanf("%d%d%d%d", &L, &R, &C, &Time);
memset(pos, false, sizeof(pos));
// int n = 0;
for(int i=1; i<=L; i++){
// getchar();//层之间的换行符
for(int j=1; j<=R; j++){
for(int k=1; k<=C; k++){
int tmp;
scanf("%d", &tmp);
map[i][j][k] = T[tmp];
}
// getchar();//行之间的换行符
}
}
if(map[L][R][C] == '#' || L+R+C-3>Time){
printf("-1\n");
continue;
}
// cout << SL << SR << SC << EL << ER << EC << endl;
if(L == 1 && R == 1 && C == 1){
cout <<0 << endl;
continue;
}
// cout << SL << SR << SC << endl;
//记录起始位置和结束位置
{
SL = 1, SR = 1, SC = 1;
}
map[1][1][1] = 'S';
{
EL = L, ER = R, EC = C;
}
map[L][R][C] = 'E';
/*
for(int i=1; i<=L ; i++){
for(int j=1; j<=R; j++){
for(int k=1; k<=C; k++){
//cin >> map[i][j][k];
printf("%c", map[i][j][k]);
//记录起始位置和结束位置
}
printf("\n");
// getchar();//行之间的换行符
}
printf("\n");
}
*/
//开始走动
bfs();
if(tmp1.t == 'E' && tmp1.step <=Time){
printf("%d\n", tmp1.step);
// printf("Escaped in %d minute(s).\n", tmp1.step);
}
else
printf("-1\n");
}
return 0;
}
/*3 4 5 20
0 0 0 0 0
0 1 1 1 0
0 1 1 0 0
1 1 1 0 1
1 1 1 1 1
1 1 1 1 1
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 1 1 1 1
1 0 1 1 1
1 1 1 1 0
*/
WA,,,刚开始改成了这样,觉得和POJ2251挺像,就照着这道题的模板改了下.....然后....目前还不知道哪有问题。
#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
int L, R, C, SL, SR, SC, EL, ER, EC, T;
char t;
const int N = 50+5;
char map[N][N][N];
bool pos[N][N][N];
int dir[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
struct Node
{
int l, r, c; //position
int step;
char t;
// bool flag;
Node(){
l = r = c = 0;
step = 0;
t = '0';
// flag = false;
}
Node(int q, int b, int d, int w, char m){
l = q, r = b, c = d, step = w, t = m;
}
};
Node tmp1;
void bfs()
{
queue<Node> q;
// Node tmp(SL, SR, SC, 0, 'S');
Node tmp;
tmp.l = SL;
tmp.r = SR;
tmp.c = SC;
tmp.step = 0;
tmp.t = 'S';
// cout << tmp.t << endl;
q.push(tmp);
pos[SL][SR][SC] = true;
while(!q.empty()){
tmp1 = q.front();
if(tmp1.t == 'E'){
return;
}
q.pop();
for(int i=0; i<6; i++){
Node tmp2;
tmp2.l = tmp1.l+dir[i][0];
tmp2.r = tmp1.r+dir[i][1];
tmp2.c = tmp1.c+dir[i][2];
// cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << " " << map[tmp2.l][tmp2.r][tmp2.c] << endl;
//judge
if((map[tmp2.l][tmp2.r][tmp2.c] == '0' || map[tmp2.l][tmp2.r][tmp2.c] == 'E') && (!pos[tmp2.l][tmp2.r][tmp2.c]) && tmp2.l<=L && tmp2.r<=R && tmp2.c<=C && tmp2.l>0 && tmp2.r>0 && tmp2.c>0){
// tmp2.flag = true;
tmp2.step = tmp1.step+1;
tmp2.t = map[tmp2.l][tmp2.r][tmp2.c];
// cout << tmp2.t << " ";
q.push(tmp2);
pos[tmp2.l][tmp2.r][tmp2.c] = true;
map[tmp2.l][tmp2.r][tmp2.c] = '1';
}
}
// cout << endl;
}
}
int main()
{
// memset(map, '1', sizeof(map));
int n;
// cin >> n;
scanf("%d", &n);
getchar();
while(n--){
// cin >> L >> R >> C >> T;
scanf("%d%d%d%d", &L, &R, &C, &T);
getchar();
// memset(map, '1', sizeof(map));
memset(pos, false, sizeof(pos));
// int n = 0;
for(int i=1; i<=L; i++){
// getchar();//层之间的换行符
for(int j=1; j<=R; j++){
for(int k=1; k<=C; k++){
//cin >> map[i][j][k];
char c=getchar();
getchar();
map[i][j][k] = c;
//记录起始位置和结束位置
}
// getchar();//行之间的换行符
}
}
map[1][1][1] = 'S';
map[L][R][C] = 'E';
/*
for(int i=1; i<=L ; i++){
for(int j=1; j<=R; j++){
for(int k=1; k<=C; k++){
//cin >> map[i][j][k];
printf("%c", map[i][j][k]);
//记录起始位置和结束位置
}
printf("\n");
// getchar();//行之间的换行符
}
printf("\n");
}
*/
SL = 1, SR = 1, SC = 1;
EL = L, ER = R, EC = C;
if(map[L][R][C] == '1' || L+R+C-3>T){
printf("-1\n");
continue;
}
// cout << SL << SR << SC << EL << ER << EC << endl;
if(L == 1 && R == 1 && C == 1){
printf("0\n");
continue;
}
//开始走动
bfs();
if(tmp1.t == 'E' && tmp1.step <= T){
printf("%d\n", tmp1.step);
}
else {
// printf("%d\n", tmp1.step);
printf("-1\n");
}
}
return 0;
}
/*3 4 5 20
0 0 0 0 0
0 1 1 1 0
0 1 1 0 0
1 1 1 0 1
1 1 1 1 1
1 1 1 1 1
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 1 1 1 1
1 0 1 1 1
1 1 1 1 0
*/
这个也WA。。。。完全照着poj2251改的....就是变了一行内容.....
#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
int L, R, C, SL, SR, SC, EL, ER, EC, Time;
char t;
char T[2] = {'.', '#'};
char map[53][53][53];
bool pos[53][53][53];
int dir[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
struct Node
{
int l, r, c; //position
int step;
char t;
// bool flag;
Node(){
l = r = c = 0;
step = 0;
t = '.';
// flag = false;
}
Node(int q, int b, int d, int w, char m){
l = q, r = b, c = d, step = w, t = m;
}
};
Node tmp1;
void bfs()
{
queue<Node> q;
// Node tmp(SL, SR, SC, 0, 'S');
Node tmp;
tmp.l = SL;
tmp.r = SR;
tmp.c = SC;
tmp.step = 0;
tmp.t = 'S';
// cout << tmp.t << endl;
q.push(tmp);
pos[SL][SR][SC] = true;
while(!q.empty()){
tmp1 = q.front();
if(tmp1.t == 'E'){
return;
}
q.pop();
for(int i=0; i<6; i++){
Node tmp2;
tmp2.l = tmp1.l+dir[i][0];
tmp2.r = tmp1.r+dir[i][1];
tmp2.c = tmp1.c+dir[i][2];
// cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << " " << map[tmp2.l][tmp2.r][tmp2.c] << endl;
//judge
if((map[tmp2.l][tmp2.r][tmp2.c] == '.'||map[tmp2.l][tmp2.r][tmp2.c] == 'E') && (!pos[tmp2.l][tmp2.r][tmp2.c]) && tmp2.l<=50 && tmp2.r<=50 && tmp2.c<=50 && tmp2.l>0 && tmp2.r>0 && tmp2.c>0){
// tmp2.flag = true;
tmp2.step = tmp1.step+1;
tmp2.t = map[tmp2.l][tmp2.r][tmp2.c];
// cout << tmp2.t << " ";
q.push(tmp2);
pos[tmp2.l][tmp2.r][tmp2.c] = true;
map[tmp2.l][tmp2.r][tmp2.c] = '#';
}
}
// cout << endl;
}
}
int main()
{
int t2;
scanf("%d", &t2);
// cin >> t2;
// memset(map, '#', sizeof(map));
while(t2--){
// cin >> L >> R >> C >> Time;
scanf("%d%d%d%d", &L, &R, &C, &Time);
memset(pos, false, sizeof(pos));
// int n = 0;
for(int i=1; i<=L; i++){
// getchar();//层之间的换行符
for(int j=1; j<=R; j++){
for(int k=1; k<=C; k++){
int tmp;
scanf("%d", &tmp);
map[i][j][k] = T[tmp];
}
// getchar();//行之间的换行符
}
}
if(map[L][R][C] == '#'){
printf("-1\n");
continue;
}
// cout << SL << SR << SC << EL << ER << EC << endl;
if(L == 1 && R == 1 && C == 1){
cout <<0 << endl;
continue;
}
// cout << SL << SR << SC << endl;
//记录起始位置和结束位置
{
SL = 1, SR = 1, SC = 1;
}
map[1][1][1] = 'S';
{
EL = L, ER = R, EC = C;
}
map[L][R][C] = 'E';
/*
for(int i=1; i<=L ; i++){
for(int j=1; j<=R; j++){
for(int k=1; k<=C; k++){
//cin >> map[i][j][k];
printf("%c", map[i][j][k]);
//记录起始位置和结束位置
}
printf("\n");
// getchar();//行之间的换行符
}
printf("\n");
}
*/
//开始走动
bfs();
if(tmp1.t == 'E' && tmp1.step <=Time){
printf("%d\n", tmp1.step);
// printf("Escaped in %d minute(s).\n", tmp1.step);
}
else
printf("-1\n");
}
return 0;
}