日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hihoCoder 1092 : Have Lunch Together

發布時間:2023/12/2 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hihoCoder 1092 : Have Lunch Together 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目大意:小hi和小ho去咖啡廳喝咖啡,咖啡廳可以看作是n * m的矩陣,每個點要么為空,要么被人、障礙物、椅子所占據,小hi和小ho想要找兩個相鄰的椅子。起初兩個人都在同一個點,求兩人到達滿足要求的椅子所移動的最少步驟。

思路:先BFS找出每個S到達每個椅子的最短路徑長度,然后遍歷每一行和每一列找出最優解。

代碼:

#include<cstdio> #include<cstring> #include<queue> #include<map> #include<cstdlib> #define INF 100000000 using namespace std;int n,m,d[10010],dir[4][2] = {-1,0,1,0,0,-1,0,1}; bool vis[10010]; char ch[110][110]; queue<int>Q;void bfs(int sx,int sy){int i,j,k;for(i=0;i<n;i++)for(j=0;j<m;j++){k = i*m + j;vis[k] = false;d[k] = INF; }k = sx * m + sy ; d[k] = 0;while(!Q.empty())Q.pop();Q.push(k);while(!Q.empty()){int x = Q.front();int tx = x/m ;int ty = x%m ; Q.pop();if(vis[x])continue;vis[x] = true;for(i=0;i<4;i++){int ex = tx + dir[i][0];int ey = ty + dir[i][1];if(ex<0 || ex>=n || ey<0 || ey>=m || ch[ex][ey] == '#' || ch[ex][ey] == 'P')continue;k = ex*m + ey;if(vis[k])continue;if(ch[ex][ey] == 'S'){d[k] = d[k] < d[x] + 1 ? d[k] : d[x] + 1;continue;}d[k] = d[k] < d[x] + 1 ? d[k] : d[x] + 1;Q.push(k);}} }int main(){int i,j,p1,p2,ans,sx,sy;bool tag;while(scanf("%d%d",&n,&m) == 2){ans = INF;bool flag = false;for(i=0;i<n;i++)scanf("%s",ch[i]);for(i=0;i<n && !flag;i++)for(j=0;j<m && !flag;j++)if(ch[i][j] == 'H'){sx = i;sy = j;flag = true;}bfs(sx,sy);int t1,t2;for(i=0;i<n;i++){p1 = p2 = INF ;for(j=0;j<m;j++){if(ch[i][j] == 'S'){int temp = d[i*m+j];if(p1 == INF)p1 = temp;else if(p2 == INF){p2 = temp;ans = ans < p1 + p2 ? ans : p1 + p2 ;p1 = p2;p2 = INF ;}}else if(ch[i][j] == 'P' || ch[i][j] == '#' || ch[i][j] == '.'){p1 = INF ;p2 = INF ;}}}for(j=0;j<m;j++){p1 = p2 = INF ;for(i=0;i<n;i++){if(ch[i][j] == 'S'){int temp = d[i*m+j];if(p1 == INF)p1 = temp;else if(p2 == INF){p2 = temp;ans = ans < p1 + p2 ? ans : p1 + p2 ;p1 = p2;p2 = INF ;}}else if(ch[i][j] == 'P' || ch[i][j] == '#' || ch[i][j] == '.'){p1 = INF ;p2 = INF ;}}}if(ans < INF)printf("%d\n",ans);elseprintf("Hi and Ho will not have lunch.\n");}return 0; }

轉載于:https://www.cnblogs.com/jxgapyw/p/4845348.html

總結

以上是生活随笔為你收集整理的hihoCoder 1092 : Have Lunch Together的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。