穿越栅栏 Overfencing
生活随笔
收集整理的這篇文章主要介紹了
穿越栅栏 Overfencing
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://www.luogu.org/problemnew/show/P1519
題解:二進制狀態壓縮+BFS
注意:洛谷上數據以‘\r\n’結尾,并非‘\n’
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int #define endl "\n" using namespace std; typedef long long ll; //typedef __int128 lll; const int N=200+10; const int M=100000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,p,l,r,u,v; int ans,cnt,flag,temp,sum; int a[N][N]; int vis[N][N]; char str[N][N]={' '}; int dir[4][2]={-1,0,0,1,1,0,0,-1}; struct node{int x,y,w;node(){};node(int i,int j,int v):x(i),y(j),w(v){} }s,tmp,f; int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//scanf("%d",&t);//while(t--){scanf("%d%d",&m,&n);getchar();getchar();for(int i=1;i<=2*n+1;i++)cin.getline(str[i]+1,1000);queue<node>q;for(int i=2;i<=2*n+1;i+=2){for(int j=2;j<=2*m+1;j+=2){int x=i/2;int y=j/2;for(int k=0;k<4;k++){if(str[i+dir[k][0]][j+dir[k][1]]!=(k%2?'|':'-')){a[x][y]+=(1<<k);}}if(str[i][1]!='|'&&!vis[x][1]){vis[x][1]=1;q.push({x,1,1});}if(str[i][2*m+1]!='|'&&!vis[x][m]){vis[x][m]=1;q.push({x,m,1});}if(str[1][j]!='-'&&!vis[1][y]){vis[1][y]=1;q.push({1,y,1});}if(str[2*n+1][j]!='-'&&!vis[n][y]){vis[n][y]=1;q.push({n,y,1});}}}ans=1;while(!q.empty()){f=q.front();q.pop();//cout<<f.x<<" "<<f.y<<" "<<f.w<<endl;for(int i=0;i<4;i++){if(a[f.x][f.y]&(1<<i)){tmp.x=f.x+dir[i][0];tmp.y=f.y+dir[i][1];tmp.w=f.w+1;if(1<=tmp.x&&tmp.x<=n&&1<=tmp.y&&tmp.y<=m&&!vis[tmp.x][tmp.y]){vis[tmp.x][tmp.y]=tmp.w;ans=max(ans,tmp.w);q.push(tmp);}}}}cout<<ans<<endl;//}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC); #endif//cout << "Hello world!" << endl;return 0; }?
總結
以上是生活随笔為你收集整理的穿越栅栏 Overfencing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两只塔姆沃斯牛 The Tamworth
- 下一篇: 回家 Bessie Come Home