上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛 C. 神奇怪兽在哪里
2017-05-21 17:22
204 查看
题意:
略.思路:
先把 ∗ 连通块附近的 . 位置存起来A={},一开始集合中先赛个P进去,然后将集合中的元素相邻两个作为起点终点,用BFS写,存个路径,有一个小优化就是在某一次BFS过程中如果已经到达过一些位置,那么就没必要再BFS到这个位置了,最终还要再将集合最后一个有效位置到P,再做一次BFS.PS: 然后没想到的是跑的还飞快…猜测数据稍微有点弱…
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef long long LL; //#pragma comment(linker, "/STACK:102400000,102400000") const int N=110; struct asd{ int x,y; }need[N*N]; int num; char ma ; bool vis ,hap ; int n,m; int px,py; int dx[4]={-1,1,0,0}; int dy[4]={0,0,1,-1}; bool Judge_in(int x,int y){ if(x<0||y<0||x>=n||y>=m) return true; return false; } void DFS(int xx,int yy){ for(int i=0;i<4;i++){ int x=dx[i]+xx; int y=dy[i]+yy; if(Judge_in(x,y)|| hap[x][y] || vis[x][y]) continue; if(ma[x][y] == '.'){ hap[x][y]=true; need[num].x=x;need[num++].y=y; } if(ma[x][y] == '*'){ vis[x][y]=true; DFS(x,y); } } } int prex ,prey ; queue<asd>q; void BFS(int sx,int sy,int ex,int ey){ asd now,nex; while(!q.empty()) q.pop(); memset(vis,false,sizeof(vis)); vis[sx][sy]=true; now.x=sx; now.y=sy; q.push(now); while(!q.empty()){ now=q.front();q.pop(); if(now.x==ex && now.y==ey) return; for(int i=0;i<4;i++){ int x=now.x+dx[i]; int y=now.y+dy[i]; if(Judge_in(x,y) || ma[x][y]=='*' || vis[x][y]) continue; vis[x][y]=true; nex.x=x; nex.y=y; prex[x][y]=now.x; prey[x][y]=now.y; q.push(nex); } } } string Getans(int tx,int ty,int x,int y){ int ox,oy; hap[x][y]=true; string temp=""; while(x!=tx || y!=ty){ ox=prex[x][y]; oy=prey[x][y]; hap[ox][oy]=false; if(ox==x){ if(oy<y) temp.push_back('R'); else temp.push_back('L'); } if(oy==y){ if(ox<x) temp.push_back('D'); else temp.push_back('U'); } x=ox; y=oy; } return temp; } int main(){ int nowx,nowy,xx,yy; while(~scanf("%d%d",&n,&m)){ bool flag1=false,flag2=false; for(int i=0;i<n;i++) { scanf("%s",ma[i]); if(flag1&&flag2) continue; for(int j=0;j<m;j++) { if(ma[i][j]=='*'&&!flag1){ xx=i;yy=j; flag1=true; } if(ma[i][j]=='P'){ px=i;py=j; flag2=true; } if(flag1&&flag2) break; } } memset(vis,false,sizeof(vis)); memset(hap,false,sizeof(hap)); vis[xx][yy]=true; num=0; need[num].x=px;need[num++].y=py; hap[px][py]=true; DFS(xx,yy); nowx=px;nowy=py; hap[nowx][nowy]=false; prex[nowx][nowy]=-1; prey[nowx][nowy]=-1; string ans="",temp; for(int i=1;i<num;i++){ if(!hap[need[i].x][need[i].y]) continue; // printf("%d %d\n",nowx,nowy); // printf("->%d %d\n",need[i].x,need[i].y); BFS(nowx,nowy,need[i].x,need[i].y); temp=Getans(nowx,nowy,need[i].x,need[i].y); nowx=need[i].x; nowy=need[i].y; reverse(temp.begin(),temp.end()); ans = ans + temp; } BFS(nowx,nowy,px,py); temp=Getans(nowx,nowy,px,py); nowx=px; nowy=py; reverse(temp.begin(),temp.end()); ans = ans + temp; cout<<ans<<endl; } return 0; }
相关文章推荐
- 「游族杯」上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛 E.章鱼哥没有女朋友
- 「游族杯」上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛
- 「游族杯」上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛 (重现) Problem D. 萝莉理论计算机科学家
- 「游族杯」上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛 A. 足球锦标赛
- 「游族杯」上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛 (重现)
- 游族杯」上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛 (重现)F. 蚂蚁
- 2017上海市高校程序设计邀请赛_F
- 2017上海市高校程序设计邀请赛_C
- 2017上海市高校程序设计邀请赛_G
- 大学生程序设计竞赛暨西南地区高校邀请赛 赛后总结
- 华南理工大学第二届“超级码力”程序设计竞赛暨广东省高校邀请赛
- k.计软联谊 「游族杯」上海市高校程序设计邀请赛(数论)
- 2017上海市高校程序设计邀请赛_I
- 2017上海市高校程序设计邀请赛_A
- 2017上海市高校程序设计邀请赛_K
- 2017上海市高校程序设计邀请赛_L
- 2017上海市高校程序设计邀请赛_E
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 F - 周末大法好
- 湖南省第九届大学生计算机程序设计竞赛
- XTU 1263 Super Resolution 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南)