Codeforces #354D (Div. 2) 暴力BFS
题目链接:题目
#include <bits/stdc++.h> using namespace std; int n,m; char mp[1005][1005]; int x1,x2,y1,y2; struct node{ int x,y,step,tim; node(int X,int Y,int Z,int T):x(X),y(Y),step(Z),tim(T){}; }; vector<pair<int,int> > D[15]; int vis[1005][1005][4]; int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; int getD(node x){ if(x.x<1||x.x>n||x.y<1||x.y>m) return -1; char c=mp[x.x][x.y]; if(c=='+'){ return 0; } else if(c=='-'){ if(x.step%2==1) return 2; else return 1; } else if(c=='|'){ if(x.step%2==1) return 1; else return 2; } else if(c=='^'){ if(x.step%4==0) return 3; else if(x.step%4==1) return 4; else if(x.step%4==2) return 6; else return 5; } else if(c=='>'){ if(x.step%4==0) return 4; else if(x.step%4==1) return 6; else if(x.step%4==2) return 5; else return 3; } else if(c=='<'){ if(x.step%4==0) return 5; else if(x.step%4==1) return 3; else if(x.step%4==2) return 4; else return 6; } else if(c=='v'){ if(x.step%4==0) return 6; else if(x.step%4==1) return 5; else if(x.step%4==2) return 3; else return 4; } else if(c=='L'){ if(x.step%4==0) return 7; else if(x.step%4==1) return 9; else if(x.step%4==2) return 8; else return 10; } else if(c=='R'){ if(x.step%4==0) return 8; else if(x.step%4==1) return 10; else if(x.step%4==2) return 7; else return 9; } else if(c=='U'){ if(x.step%4==0) return 9; else if(x.step%4==1) return 8; else if(x.step%4==2) return 10; else return 7; } else if(c=='D'){ if(x.step%4==0) return 10; else if(x.step%4==1) return 7; else if(x.step%4==2) return 9; else return 8; } else if(c=='*'){ return 11; } } bool OK(int a,int b,int c){ if(a==-1||b==-1) return false; if(c==0) return (b==0||b==2||b==6||b==9||b==8||b==7)&&(a==0||a==2||a==3||a==10||a==8||a==7); else if(c==1) return (b==0||b==2||b==3||b==10||b==8||b==7)&&(a==0||a==2||a==6||a==9||a==8||a==7); else if(c==2) return (b==0||b==1||b==5||b==10||b==8||b==9)&&(a==0||a==1||a==4||a==10||a==9||a==7); else if(c==3) return (b==0||b==1||b==4||b==9||b==10||b==7)&&(a==0||a==1||a==5||a==10||a==8||a==9); } int BFS(){ queue<node> Q; Q.push(node(x1,y1,0,0)); while(!Q.empty()){ node t=Q.front(); Q.pop(); if(t.x<1||t.x>n||t.y<1||t.y>m) continue; if(t.x==x2&&t.y==y2) return t.tim; if(vis[t.x][t.y][t.step%4]==1) continue; vis[t.x][t.y][t.step%4]=1; int v=getD(t); Q.push(node(t.x,t.y,t.step+1,t.tim+1)); for(int i=0;i<4;i++){ int k=getD(node(t.x+dir[i][0],t.y+dir[i][1],t.step,0)); if(OK(v,k,i)) Q.push(node(t.x+dir[i][0],t.y+dir[i][1],t.step,t.tim+1)); } } return -1; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) cin>>mp[i][j]; } scanf("%d%d%d%d",&x1,&y1,&x2,&y2); cout<<BFS()<<endl; return 0; }
题意,一个矩阵,一堆门有不同的意思,详细见题目,你可以花费一单位时间来旋转所有的门。也可以从两个全开的门之间到另一个房间。。
1000*1000的数据范围很好理解就是一个暴力搜索题目,就是代码比较恶心。
因为判断联通什么的还要旋转多少次都要各种if-----ififififif。。总之编码能力还是比较弱,并没有1a。。继续努力吧蒟蒻。。
分类:
搜索
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?