输出最少走几步。
11
#include <iostream> using namespace std; int c,d,sum; int vis[9][9]; int map[9][9]= { 1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1 }; void dfs(int i,int j,int ans) { if(i<0||j<0||i>8||j>8||map[i][j]||vis[i][j]||ans>=sum) return ; if(i==c&&j==d) { sum=ans; return ; } vis[i][j]=1; dfs(i-1,j,ans+1); dfs(i+1,j,ans+1); dfs(i,j+1,ans+1); dfs(i,j-1,ans+1); vis[i][j]=0; } int main() { int n; int a,b; cin>>n; while(n--) { cin>>a>>b>>c>>d; sum=100; dfs(a,b,0); cout<<sum<<endl; } return 0; }