【笔记】
一开始写这道题的时候,总是提示超内存。
后来发现是是判断H的位置是否合法那块代码写错位置了,导致运行的时候多扫了很多东西。
先贴正确答案,后面贴错误的。
// Problem#: 1215
// Submission#: 4560607
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <queue>
#include <map>
#include <cstring>
using namespace std;
char mymap[22][22];
int n, m;
int Hdirection[4];
int directionX[4] = { -1,1,0,0 };
int directionY[4] = { 0,0,-1,1 };
bool isVisited[22][22][22][22];
struct Point {
int x;
int y;
int time;
int hx;
int hy;
}Player;
//void printMap() {
// for (int i = 0; i < n; i++){
// for (int k = 0; k < m; k++) {
// cout << mymap[i][k];
// }
// cout << endl;
// }
// cout << "==========" << endl;
//}
bool isValid(Point temp) {
if (mymap[temp.x][temp.y] == '!' || mymap[temp.hx][temp.hy] == '!' || mymap[temp.x][temp.y] == '#') {
return false;
}
return true;
}
int run() {
queue<Point> q;
q.push(Player);
isVisited[Player.x][Player.y][Player.hx][Player.hy] = true;
while (!q.empty()) {
Point now = q.front();
//cout << "----" << endl;
//cout << now.x << " " << now.y << " " << now.hx << " " << now.hy << endl;
//cout << "Move: " << now.time << endl;
q.pop();
if (now.time > 255)
break;
for (int i = 0; i < 4; i++) {
Point temp = now;
//cout << "H: " << temp.hx << " " << temp.hy << endl;
temp.x += directionX[i];
temp.y += directionY[i];
temp.hx += directionX[Hdirection[i]];
temp.hy += directionY[Hdirection[i]];
temp.time++;
//cout << "powerTo: " << powerToChangeDirection[i] << endl;
//cout << "now: " << temp.hx << " " << temp.hy << endl;
if (mymap[temp.hx][temp.hy] == '#') {
temp.hx = now.hx;
temp.hy = now.hy;
}
if (!isVisited[temp.x][temp.y][temp.hx][temp.hy] && isValid(temp)) {
if (now.x == temp.hx && now.y == temp.hy && now.hx == temp.x && now.hy == temp.y) {
return temp.time;
}
else if (temp.x == temp.hx && temp.y == temp.hy) {
return temp.time;
}
isVisited[temp.x][temp.y][temp.hx][temp.hy] = true;
q.push(temp);
}
}
}
return -1;
}
int main() {
cin >> n >> m;
for (int i = 0; i < 22; i++)
for (int k = 0; k < 22; k++)
mymap[i][k] = '#';
//printMap();
for (int i = 0; i < n; i++)
for (int k = 0; k < m; k++){
cin >> mymap[i][k];
if (mymap[i][k] == 'P') {
Player.x = i;
Player.y = k;
Player.time = 0;
}
else if (mymap[i][k] == 'H') {
Player.hx = i;
Player.hy = k;
}
}
//printMap();
memset(isVisited, false, sizeof(isVisited));
char temp;
map<char, int> change;
change['N'] = 0;
change['S'] = 1;
change['W'] = 2;
change['E'] = 3;
for (int i = 0; i < 4;i++) {
cin >> temp;
Hdirection[i] = change[temp];
}
int res = run();
if (res != -1)cout << res << endl;
else cout << "Impossible" << endl;
//system("PAUSE");
}
有误的解答
// Problem#: 1215
// Submission#: 4560584
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <queue>
#include <map>
#include <cstring>
using namespace std;
char mymap[22][22];
int n, m;
int Hdirection[4];
int directionX[4] = { -1,1,0,0 };
int directionY[4] = { 0,0,-1,1 };
bool isVisited[22][22][22][22];
struct Point {
int x;
int y;
int time;
int hx;
int hy;
}Player;
//void printMap() {
// for (int i = 0; i < n; i++){
// for (int k = 0; k < m; k++) {
// cout << mymap[i][k];
// }
// cout << endl;
// }
// cout << "==========" << endl;
//}
bool isValid(Point temp) {
if (mymap[temp.x][temp.y] == '!' || mymap[temp.hx][temp.hy] == '!' || mymap[temp.x][temp.y] == '#') {
return false;
}
return true;
}
void run() {
queue<Point> q;
q.push(Player);
while (!q.empty()) {
Point now = q.front();
//cout << "----" << endl;
//cout << now.x << " " << now.y << " " << now.hx << " " << now.hy << endl;
//cout << "Move: " << now.time << endl;
q.pop();
if (now.x == now.hx && now.hy == now.y) {
cout << now.time << endl;
return;
}
isVisited[now.x][now.y][now.hx][now.hy] = true;
if (now.time > 255)
break;
for (int i = 0; i < 4; i++) {
Point temp = now;
//cout << "H: " << temp.hx << " " << temp.hy << endl;
temp.x += directionX[i];
temp.y += directionY[i];
temp.hx += directionX[Hdirection[i]];
temp.hy += directionY[Hdirection[i]];
//cout << "powerTo: " << powerToChangeDirection[i] << endl;
//cout << "now: " << temp.hx << " " << temp.hy << endl;
if (isValid(temp) && !isVisited[temp.x][temp.y][temp.hx][temp.hy]) {
if (mymap[temp.hx][temp.hy] == '#') {
temp.hx = now.hx;
temp.hy = now.hy;
}
if (now.x == temp.hx && now.y == temp.hy && now.hx == temp.x && now.hy == temp.y) {
now.time++;
cout << now.time << endl;
return;
}
temp.time++;
isVisited[temp.x][temp.y][temp.hx][temp.hy] = true;
q.push(temp);
}
}
}
cout << "Impossible" << endl;
return;
}
int main() {
cin >> n >> m;
for (int i = 0; i < 22; i++)
for (int k = 0; k < 22; k++)
mymap[i][k] = '#';
//printMap();
for (int i = 0; i < n; i++)
for (int k = 0; k < m; k++){
cin >> mymap[i][k];
if (mymap[i][k] == 'P') {
Player.x = i;
Player.y = k;
Player.time = 0;
}
else if (mymap[i][k] == 'H') {
Player.hx = i;
Player.hy = k;
}
}
//printMap();
memset(isVisited, false, sizeof(isVisited));
char temp;
map<char, int> change;
change['N'] = 0;
change['S'] = 1;
change['W'] = 2;
change['E'] = 3;
for (int i = 0; i < 4;i++) {
cin >> temp;
Hdirection[i] = change[temp];
}
run();
//system("PAUSE");
}