2016-8-16未命名文件

    xiaoxiao2026-05-04  5

    #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define ll long long #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("in.txt", "r", stdin); #define Write() freopen("out.txt", "w", stdout); #define M 137 #define N 55 using namespace std; typedef pair<int,int> P; const int inf = 0x7f7f7f7f; const int mod = 1000000007; int main() { freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int dmove[3]={1,2,3}; int dzhuan[4]={0,-1,1,2}; int n,m; scanf("%d%d",&n,&m); int tu[n][m]; int d[n][m]; for(int i=0;i<n;i++){ for (int j=0;j<m;j++) scanf("%d",&tu[i][j]); } int sx,sy,gx,gy; char sfang[3]; int fang; scanf("%d%d%d%d%s",&sx,&sy,&gx,&gy,sfang); for(int i=0;i<n;i++) { for(int j=0;j<m;j++){ printf("%d ",tu[i][j]); } printf("\n"); } if(sfang[0]=='N') fang=0; if(sfang[0]=='E') fang=1; if(sfang[0]=='S') fang=2; if(sfang[0]=='W') fang=3; queue<P>que; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ d[i][j]=inf; } } que.push(P(sx,sy)); d[sx][sy]=0; while(que.size()){ P p=que.front();que.pop(); if(p.first==gx&&p.second==gy) break; for(int i=0;i<4;i++){ fang=(fang+dzhuan[i])%4; for(int j=0;j<3;j++){ int nx,ny; if(fang==0) nx=p.first,ny=p.second-dmove[j]; else if(fang==1) nx=p.first+dmove[j],ny=p.second; else if(fang==2) nx=p.first,ny=p.second+dmove[j]; else if(fang==3) nx=p.first-dmove[j],ny=p.second; if(nx>=1&&nx<(n-1)&&ny>=1&&ny<(m-1)&&tu[nx][ny]!=1&&d[nx][ny]==inf){ que.push(P(nx,ny)); d[nx][ny]=d[p.first][p.second]+1+abs(dzhuan[i]); } } } } printf("%d ********\n",d[sx][sy]); if(d[gx][gy]==inf) printf("-1\n"); else printf("%d\n",d[gx][gy]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++){ printf("%d ",d[i][j]); } cout<<endl; } return 0; }

    欢迎使用 {小书匠}(xiaoshujiang)编辑器,您可以通过==设置==里的修改模板来改变新建文章的内容。

    #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define ll long long #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("in.txt", "r", stdin); #define Write() freopen("out.txt", "w", stdout); #define M 137 #define N 55 using namespace std; typedef pair<int,int> P; const int inf = 0x7f7f7f7f; const int mod = 1000000007; int main() { freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int dmove[3]={1,2,3}; int dzhuan[4]={0,-1,1,2}; int n,m; scanf("%d%d",&n,&m); int tu[n][m]; int d[n][m]; for(int i=0;i<n;i++){ for (int j=0;j<m;j++) scanf("%d",&tu[i][j]); } int sx,sy,gx,gy; char sfang[3]; int fang; scanf("%d%d%d%d%s",&sx,&sy,&gx,&gy,sfang); for(int i=0;i<n;i++) { for(int j=0;j<m;j++){ printf("%d ",tu[i][j]); } printf("\n"); } if(sfang[0]=='N') fang=0; if(sfang[0]=='E') fang=1; if(sfang[0]=='S') fang=2; if(sfang[0]=='W') fang=3; queue<P>que; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ d[i][j]=inf; } } que.push(P(sx,sy)); d[sx][sy]=0; while(que.size()){ P p=que.front();que.pop(); if(p.first==gx&&p.second==gy) break; for(int i=0;i<4;i++){ fang=(fang+dzhuan[i])%4; for(int j=0;j<3;j++){ int nx,ny; if(fang==0) nx=p.first,ny=p.second-dmove[j]; else if(fang==1) nx=p.first+dmove[j],ny=p.second; else if(fang==2) nx=p.first,ny=p.second+dmove[j]; else if(fang==3) nx=p.first-dmove[j],ny=p.second; if(nx>=1&&nx<(n-1)&&ny>=1&&ny<(m-1)&&tu[nx][ny]!=1&&d[nx][ny]==inf){ que.push(P(nx,ny)); d[nx][ny]=d[p.first][p.second]+1+abs(dzhuan[i]); } } } } printf("%d ********\n",d[sx][sy]); if(d[gx][gy]==inf) printf("-1\n"); else printf("%d\n",d[gx][gy]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++){ printf("%d ",d[i][j]); } cout<<endl; } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1309362.html
    最新回复(0)