标签:nyoj 483
2 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3
4 -1
| 896378 | 长木 | Nightmare | Accepted |
4 | 308 | C/C++ | 06-15 21:03:39 |
| 896368 | 长木 | Nightmare | WrongAnswer | -- | -- | C/C++ | 06-15 20:54:57 |
| 896363 | 长木 | Nightmare | WrongAnswer | -- | -- | C/C++ | 06-15 20:49:37 |
| 896360 | 长木 | Nightmare | WrongAnswer | -- | -- | C/C++ | 06-15 20:46:04 |
| 896339 | 长木 | Nightmare | WrongAnswer | -- | -- | C/C++ | 06-15 20:14:06 |
| 896333 | 长木 | Nightmare | WrongAnswer | -- | -- | C/C++ | 06-15 20:02:33 |
| 896306 | 长木 | Nightmare | WrongAnswer | -- | -- | C/C++ | 06-15 19:06:14 |
AC:
#include <stdio.h>
#include <queue>
using std::queue;
int map[10][10], t, m, n, id;
int mov[][2] = {1, 0, -1, 0, 0, 1, 0, -1};
struct Node{
int x, y, steps, time;
} start;
queue<Node> Q;
int check(int i, int j){
if(i < 0 || j < 0 || i >= m || j >= n)
return 0;
return map[i][j];
}
int BFS(){
Node now, temp;
while(!Q.empty()){
now = Q.front();
Q.pop();
if(now.time == 1) continue; //cut
for(int i = 0; i < 4; ++i){
temp = now;
temp.x += mov[i][0];
temp.y += mov[i][1];
--temp.time; ++temp.steps;
if(check(temp.x, temp.y)){
if(map[temp.x][temp.y] == 3) return temp.steps;
else if(map[temp.x][temp.y] == 4){ //防止无限次充电
map[temp.x][temp.y] = 1;
temp.time = 6;
}
Q.push(temp);
}
}
}
return -1;
}
int main(){
scanf("%d", &t);
while(t--){
scanf("%d%d", &m, &n);
start.steps = 0;
start.time = 6;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j){
scanf("%d", &map[i][j]);
if(map[i][j] == 2){
start.x = i; start.y = j;
}
}
while(!Q.empty()) Q.pop();
Q.push(start);
printf("%d\n", BFS());
}
return 0;
} WA:
#include <stdio.h>
int map[10][10], t, m, n, id;
int mov[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
struct Node{
int x, y, steps, time;
} Q[70], start;
int check(int i, int j){
if(i < 0 || j < 0 || i >= m || j >= n)
return 0;
return map[i][j];
}
int BFS(){
Node now, temp;
while(id){
now = Q[--id]; //这哪是出队啊,⊙﹏⊙b汗
if(map[now.x][now.y] == 3) return now.steps;
if(now.time == 1) continue; //cut
for(int i = 0; i < 4; ++i){
temp = now;
temp.x += mov[i][0];
temp.y += mov[i][1];
--temp.time; ++temp.steps;
if(check(temp.x, temp.y)){
if(map[temp.x][temp.y] == 4){ //防止无限次充电
map[temp.x][temp.y] = 1; //不能赋值为0,否则可能无限循环
temp.time = 6;
}
Q[id++] = temp;
}
}
}
return -1;
}
int main(){
scanf("%d", &t);
while(t--){
scanf("%d%d", &m, &n);
start.steps = id = 0;
start.time = 6;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j){
scanf("%d", &map[i][j]);
if(map[i][j] == 2){
start.x = i; start.y = j;
}
}
Q[id++] = start;
printf("%d\n", BFS());
}
return 0;
}NYOJ 483 Nightmare 【广搜】+【无标记】,布布扣,bubuko.com
标签:nyoj 483
原文地址:http://blog.csdn.net/chang_mu/article/details/31032479