用了超多if,不知道要怎麼減少if,所以打了超多字
#include <bits/stdc++.h>
using namespace std;
int n, m;
int bfs(vector<string> map,int x1,int y1,int x2,int y2){
queue<pair<pair<int,int>,int>>a;
a.push(make_pair(make_pair(x1,y1),0));
while(!a.empty()){
int x = a.front().first.first;
int y = a.front().first.second;
int t=a.front().second;
a.pop();
if(x==x2&&y==y2){
return t;
}
if(x-1>=0&&y-1>=0&&map[x-1][y-1]=='.'){
map[x-1][y-1]=t+1+'0';
a.push(make_pair(make_pair(x-1,y-1),t+1));
}
if(y-1>=0&&map[x][y-1]=='.'){
map[x][y-1]=t+1+'0';
a.push(make_pair(make_pair(x,y-1),t+1));
}
if(x+1<n&&y-1>=0&&map[x+1][y-1]=='.'){
map[x+1][y-1]=t+1+'0';
a.push(make_pair(make_pair(x+1,y-1),t+1));
}
if(x-1>=0&&map[x-1][y]=='.'){
map[x-1][y]=t+1+'0';
a.push(make_pair(make_pair(x-1,y),t+1));
}
if(x+1<n&&map[x+1][y]=='.'){
map[x+1][y]=t+1+'0';
a.push(make_pair(make_pair(x+1,y),t+1));
}
if(x-1>=0&&y+1<m&&map[x-1][y+1]=='.'){
map[x-1][y+1]=t+1+'0';
a.push(make_pair(make_pair(x-1,y+1),t+1));
}
if(y+1<m&&map[x][y+1]=='.'){
map[x][y+1]=t+1+'0';
a.push(make_pair(make_pair(x,y+1),t+1));
}
if(x+1<n&&y+1<m&&map[x+1][y+1]=='.'){
map[x+1][y+1]=t+1+'0';
a.push(make_pair(make_pair(x+1,y+1),t+1));
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> m;
int x1,y1,x2,y2;
vector<string> map(n);
for(int i=0;i<n;i++){
cin>>map[i];
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(map[i][j]=='Z'){
map[i][j]='x';
if(i-2>=0&&j-1>=0&&map[i-2][j-1]=='.'){
map[i-2][j-1]='x';
}
if(i-1>=0&&j-2>=0&&map[i-1][j-2]=='.'){
map[i-1][j-2]='x';
}
if(i+1<n&&j-2>=0&&map[i+1][j-2]=='.'){
map[i+1][j-2]='x';
}
if(i+2<n&&j-1>=0&&map[i+2][j-1]=='.'){
map[i+2][j-1]='x';
}
if(i+2<n&&j+1<m&&map[i+2][j+1]=='.'){
map[i+2][j+1]='x';
}
if(i+1<n&&j+2<m&&map[i+1][j+2]=='.'){
map[i+1][j+2]='x';
}
if(i-1>=0&&j+2<m&&map[i-1][j+2]=='.'){
map[i-1][j+2]='x';
}
if(i-2>=0&&j+1<m&&map[i-2][j+1]=='.'){
map[i-2][j+1]='x';
}
}else if(map[i][j]=='A'){
x1=i;
y1=j;
}else if(map[i][j]=='B'){
x2=i;
y2=j;
}
}
}
map[x1][y1]='0';
map[x2][y2]='.';
int a=bfs(map, x1, y1, x2, y2);
if(a==-1){
cout<<"King Peter, you can't go now!\n";
}else{
cout<<"Minimal possible length of a trip is "<<a<<'\n';
}
}
}
用了超多if,不知道要怎麼減少if,所以打了超多字
#include
using namespace std;
int n, m;
int bfs(vector map,int x1,int y1,int x2,int y2){
queue,int>>a;
a.push(make_pair(make_pair(x1,y1),0));
while(!a.empty()){
int x = a.front().first.first;
int y = a.front().first.second;
int t=a.front().second;
a.pop();
if(x==x2&&y==y2){
return t;
}
if(x-1>=0&&y-1>=0&&map[x-1][y-1]=='.'){
map[x-1][y-1]=t+1+'0';
a.push(make_pair(make_pair(x-1,y-1),t+1));
}
if(y-1>=0&&map[x][y-1]=='.'){
map[x][y-1]=t+1+'0';
a.push(make_pair(make_pair(x,y-1),t+1));
}
if(x+1=0&&map[x+1][y-1]=='.'){
map[x+1][y-1]=t+1+'0';
a.push(make_pair(make_pair(x+1,y-1),t+1));
}
if(x-1>=0&&map[x-1][y]=='.'){
map[x-1][y]=t+1+'0';
a.push(make_pair(make_pair(x-1,y),t+1));
}
if(x+1
map[x+1][y]=t+1+'0';
a.push(make_pair(make_pair(x+1,y),t+1));
}
if(x-1>=0&&y+1
map[x-1][y+1]=t+1+'0';
a.push(make_pair(make_pair(x-1,y+1),t+1));
}
if(y+1
map[x][y+1]=t+1+'0';
a.push(make_pair(make_pair(x,y+1),t+1));
}
if(x+1
map[x+1][y+1]=t+1+'0';
a.push(make_pair(make_pair(x+1,y+1),t+1));
}
}
return -1;
}int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> m;
int x1,y1,x2,y2;
vector map(n);
for(int i=0;i
cin>>map[i];
}
for(int i=0;i
for(int j=0;j
if(map[i][j]=='Z'){
map[i][j]='x';
if(i-2>=0&&j-1>=0&&map[i-2][j-1]=='.'){
map[i-2][j-1]='x';
}
if(i-1>=0&&j-2>=0&&map[i-1][j-2]=='.'){
map[i-1][j-2]='x';
}
if(i+1=0&&map[i+1][j-2]=='.'){
map[i+1][j-2]='x';
}
if(i+2=0&&map[i+2][j-1]=='.'){
map[i+2][j-1]='x';
}
if(i+2
map[i+2][j+1]='x';
}
if(i+1
map[i+1][j+2]='x';
}
if(i-1>=0&&j+2
map[i-1][j+2]='x';
}
if(i-2>=0&&j+1
map[i-2][j+1]='x';
}
}else if(map[i][j]=='A'){
x1=i;
y1=j;
}else if(map[i][j]=='B'){
x2=i;
y2=j;
}
}
}
map[x1][y1]='0';
map[x2][y2]='.';
int a=bfs(map, x1, y1, x2, y2);
if(a==-1){
cout<<"King Peter, you can't go now!\n";
}else{
cout<<"Minimal possible length of a trip is "< }
}
}
我的方式
int horsedRow[] = {2, 2, 1, 1, -1, -1, -2, -2}, horsedCol[] = {1, -1, 2, -2, 2, -2, 1, -1};
int kingdRow[] = {1, 1, 1, -1, -1, -1, 0, 0}, kingdCol[] = {1, 0, -1, 1, 0, -1, 1, -1};
陣列儲存要拜訪的方向
for(int k = 0; k < 8; ++k) {
int newRow = i + horsedRow[k], newCol = j + horsedCol[k];
if(newRow >= 0 && newRow < M && newCol >= 0 && newCol < N && forest[newRow][newCol] == '.')
forest[newRow][newCol] = 'z';
}
用迴圈更新方向