70分re3、7,wa10求助(总是算多)
查看原帖
70分re3、7,wa10求助(总是算多)
884897
Accepted3927楼主2024/1/14 20:42
#include<bits/stdc++.h>
using namespace std;
int x,y,step[25][25];
bool vis[1000][1000];
queue<int> qx;
queue<int> qy;
int dx[12]={2,2,-2,-2,-1,-1,1,1,-2,-2,2,2};
int dy[12]={2,-2,2,-2,-2,2,-2,2,1,-1,1,-1};
bool flag;
int main(){
	cin>>x>>y;
	qx.push(x);
	qy.push(y);
	vis[x][y]=1;
	flag=0;
	while(!qx.empty()){
		int hx=qx.front(),hy=qy.front();
		qx.pop();
		qy.pop();
		for(int i=0;i<12;i++){
			int ex=hx+dx[i];
			int ey=hy+dy[i]; 
			if(ex>0 && ey>0 && vis[ex][ey]==0){
				if(ex==1 && ey==1){
					flag=1;
					cout<<step[hx][hy]+1<<endl;
					break;
				}
				vis[ex][ey]=1;
				step[ex][ey]=step[hx][hy]+1;
				qx.push(ex);
				qy.push(ey);
			}
		}
		if(flag==1) break;
	}
	while(!qx.empty()){
		qx.pop();
	}
	while(!qy.empty()){
		qy.pop();
	}
	memset(vis,0,sizeof(vis));
	memset(step,0,sizeof(step));
	cin>>x>>y;
	qx.push(x);
	qy.push(y);
	vis[x][y]=1;
	flag=0;
	while(!qx.empty()){
		int hx=qx.front(),hy=qy.front();
		qx.pop();
		qy.pop();
		for(int i=0;i<12;i++){
			int ex=hx+dx[i];
			int ey=hy+dy[i]; 
			if(ex>0 && ey>0 && vis[ex][ey]==0){
				if(ex==1 && ey==1){
					flag=1;
					cout<<step[hx][hy]+1<<endl;
					break;
				}
				vis[ex][ey]=1;
				step[ex][ey]=step[hx][hy]+1;
				qx.push(ex);
				qy.push(ey);
			}
		}
		if(flag==1) break;
	}
	return 0;
}

2024/1/14 20:42
加载中...