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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

CodeForces 173B Chamber of Secrets spfa

發布時間:2025/3/15 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces 173B Chamber of Secrets spfa 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Chamber of Secrets

題目連接:

http://codeforces.com/problemset/problem/173/B

Description

"The Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.

The Chamber of Secrets is an n?×?m rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below.

The left light ray passes through a regular column, and the right ray — through the magic column.
The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position.

This figure illustrates the first sample test.
Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.

Input

The first line of the input contains two integer numbers n and m (2?≤?n,?m?≤?1000). Each of the next n lines contains m characters. Each character is either "." or "#" and represents one cell of the Chamber grid. It's "." if the corresponding cell is empty and "#" if it's a regular column.

Output

Print the minimum number of columns to make magic or -1 if it's impossible to do.

Sample Input

3 3

.#.

...

.#.

Sample Output

2

Hint

題意

給你一個n*m的矩陣,你從(1,0)位置射出一個平行于x軸的光,遇到#號的時候,你可以選擇轉彎

然后你要要到達(n,m+1)這個位置

問你最少用多少個#

題解:

直接spfa就可以過

你可以把這個圖優化成二分圖

然后再跑spfa

代碼

#include<bits/stdc++.h> using namespace std;vector<int>H[1200]; vector<int>L[1200]; int d[1200][3]; int vis[1200][3]; int n,m; char str[1200][1200]; struct node {int x,y; }; void bfs(int x,int y) {queue<node> Q;for(int i=0;i<1200;i++)for(int j=0;j<3;j++)d[i][j]=1e9;node now;now.x=x,now.y=y;d[now.x][now.y]=0;vis[now.x][now.y]=1;Q.push(now);while(!Q.empty()){now = Q.front();Q.pop();vis[now.x][now.y]=0;if(now.y==0){for(int i=0;i<H[now.x].size();i++){node next = now;next.y = 1;next.x = H[now.x][i];if(d[next.x][next.y]>d[now.x][now.y]+1){d[next.x][next.y]=d[now.x][now.y]+1;if(vis[next.x][next.y])continue;vis[next.x][next.y]=1;Q.push(next);}}}if(now.y==1){for(int i=0;i<L[now.x].size();i++){node next = now;next.y = 0;next.x = L[now.x][i];if(d[next.x][next.y]>d[now.x][now.y]+1){d[next.x][next.y]=d[now.x][now.y]+1;if(vis[next.x][next.y])continue;Q.push(next);}}}}if(d[n][0]>1e8)printf("-1\n");elseprintf("%d\n",d[n][0]);return; } int main() {scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%s",str[i]+1);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(str[i][j]=='#')H[i].push_back(j),L[j].push_back(i);bfs(1,0); }

轉載于:https://www.cnblogs.com/qscqesze/p/5146207.html

總結

以上是生活随笔為你收集整理的CodeForces 173B Chamber of Secrets spfa的全部內容,希望文章能夠幫你解決所遇到的問題。

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