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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

双端队列 BFS + Chamber of Secrets CodeForces - 173B

發布時間:2023/12/4 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双端队列 BFS + Chamber of Secrets CodeForces - 173B 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

一個 n×mn\times mn×m 的圖,現在有一束激光從左上角往右邊射出,每遇到 ‘#’,你可以選擇光線往四個方向射出,或者什么都不做,問最少需要多少個 ‘#’ 往四個方向射出才能使光線在第 n 行往右邊射出。

題目:

“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.

Examples

Input

3 3
.#.

.#.

Output

2

Input

4 3
##.

.#.
.#.

Output

2

Note

The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.

分析:

1.此題目正解不是 0-1 BFS 但是適用 0-1 BFS 可以不需要思考過程,賽時許多大佬都是這么做的。
2.做法很簡單,一個方向射出不需要花費(0),而往四個方向射出需要花費(1),然后直接來就可以了。
3.復習雙端隊列deque
std::deque 是 STL 提供的 雙端隊列 數據結構。能夠提供線性復雜度的插入和刪除,以及常數復雜度的隨機訪問。

  • 構造函數
    參見如下代碼(假設你已經 using 了 std 命名空間相關類型):
// 1. 定義一個int類型的空雙端隊列 v0 deque<int> v0; // 2. 定義一個int類型的雙端隊列 v1,并設置初始大小為10; 線性復雜度 deque<int> v1(10); // 3. 定義一個int類型的雙端隊列 v2,并初始化為10個1; 線性復雜度 deque<int> v2(10, 1); // 4. 復制已有的雙端隊列 v1; 線性復雜度 deque<int> v3(v1); // 5. 創建一個v2的拷貝deque v4,其內容是v4[0]至v4[2]; 線性復雜度 deque<int> v4(v2.begin(), v2.begin() + 3); // 6. 移動v2到新創建的deque v5,不發生拷貝; 常數復雜度; 需要 C++11 deque<int> v5(std::move(v2));
  • 元素訪問
    與 vector 一致,但無法訪問底層內存。其高效的元素訪問速度可參考實現細節部分。
函數作用
at()返回容器中指定位置元素的引用,執行越界檢查,常數復雜度。
operator[]返回容器中指定位置元素的引用。不執行越界檢查,常數復雜度。
front()返回首元素的引用。
back()返回末尾元素的引用。
  • 元素增刪及修改
    與 vector 一致,并額外有向隊列頭部增加元素的函數。
函數作用
clear()清除所有元素
insert()支持在某個迭代器位置插入元素、可以插入多個。復雜度與 pos 與兩端距離較小者成線性。
erase()刪除某個迭代器或者區間的元素,返回最后被刪除的迭代器。復雜度與 insert 一致。
push_front()在頭部插入一個元素,常數復雜度。
pop_front()刪除頭部元素,常數復雜度。
push_back()在末尾插入一個元素,常數復雜度。
pop_back()刪除末尾元素,常數復雜度。
swap()與另一個容器進行交換,此操作是 常數復雜度 而非線性的。
  • deque 的實現細節
    deque 通常的底層實現是多個不連續的緩沖區,而緩沖區中的內存是連續的。而每個緩沖區還會記錄首指針和尾指針,用來標記有效數據的區間。當一個緩沖區填滿之后便會在之前或者之后分配新的緩沖區來存儲更多的數據。

AC代碼:

#include<stdio.h> #include<string.h> #include<deque> #include<iostream> using namespace std; const int M=1e3+10; const int inf=0x3f3f3f3f; int n,m; char mp[M][M]; int f[M][M][10]; int c[4][2]= {1,0,-1,0,0,1,0,-1}; deque<int>q; void Add_front(int x,int y,int dir,int step) {if(step<f[x][y][dir]){q.push_front(dir);//放在首位的需要倒著放入q.push_front(y);q.push_front(x);f[x][y][dir]=step;} } void Add_back(int x,int y,int dir,int step) {if(step<f[x][y][dir]){q.push_back(x);q.push_back(y);q.push_back(dir);f[x][y][dir]=step;} } int main() {cin>>n>>m;for(int i=0; i<n; i++)cin>>mp[i];for(int i=0; i<n; i++)for(int j=0; j<m; j++)for(int k=0; k<4; k++)f[i][j][k]=inf;Add_front(n-1,m-1,3,0);while(!q.empty()){int x=q[0];int y=q[1];int dir=q[2];q.pop_front();q.pop_front();q.pop_front();int xx=x+c[dir][0];int yy=y+c[dir][1];if(xx>=0&&yy>=0&&xx<n&&yy<m)Add_front(xx,yy,dir,f[x][y][dir]);if(mp[x][y]=='#'){for(int i=0; i<4; i++){if(i!=dir)Add_back(x,y,i,f[x][y][dir]+1);}}}if(f[0][0][3]==inf)cout<<"-1"<<endl;elsecout<<f[0][0][3]<<endl;return 0; }

總結

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

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

主站蜘蛛池模板: 亚洲av无一区二区三区久久 | 美日韩在线 | 国产剧情精品在线 | 亚洲永久精品视频 | 亚洲中文无码久久 | 最污的网站 | 小宝贝真紧h军人h | 中文字幕精品三级久久久 | av一区二区三区在线观看 | 国产美女视频免费观看下载软件 | 国产精品丝袜一区 | 欧美在线二区 | 日韩av免费在线播放 | 欧美性粗暴 | 二区国产 | 午夜激情网 | 麻豆传媒mv| 国内一区二区视频 | 欧美精品a区 | 久久影片 | 国产三级在线免费 | 国产精品福利小视频 | 天天曰天天 | 午夜爱爱网 | 麻豆精品视频在线 | 国产一区二区三区精品愉拍 | 最新中文字幕在线观看 | 99re久久精品国产 | 一本视频在线 | 一级黄色片一级黄色片 | 亚洲va国产va天堂va久久 | 婷婷影音| 人人干人人做 | 5a毛片 | 三年电影在线观看 | 日本三级韩国三级三级a级中文 | 欧美日韩国产在线一区 | 天堂网av2014 | 四虎影院一区 | 丰满大肥婆肥奶大屁股 | 91a视频| 日韩欧美一区二区三区久久婷婷 | av免费网页 | 亚洲欧洲日韩av | 玖草在线| 国产日韩欧美在线观看视频 | 成年人免费看视频 | 一区二区三区视频免费观看 | 九七久久 | 中文在线日本 | 息与子五十路翔田千里 | 成人国产精品免费观看 | 国产特黄级aaaaa片免 | 久久精品黄aa片一区二区三区 | 欧美老肥妇做.爰bbww视频 | 天天综合天天 | 高清黄色一级片 | 激情福利视频 | 别揉我奶头一区二区三区 | 性欧美videos高清hd4k | av影片在线看 | 亚洲成人91| 欧美老肥妇做爰bbww | 午夜电影天堂 | 国产免费久久精品国产传媒 | 成人综合婷婷国产精品久久 | 手机av在线免费 | 十大污视频 | 91精品国产99久久久久久 | 免费黄色小视频在线观看 | 91草草草 | 亚洲熟妇无码爱v在线观看 九色福利 | 在线网站av | 精品福利一区二区三区 | 奇米影视狠狠干 | 久久久久久久久国产 | gav久久| 在线国产视频一区 | 欧美人成在线 | 91沈先生在线 | www.色综合.com | 色综合一区| 国产精品久久久久久久久久东京 | 女上男下动态图 | 中文字幕免费播放 | 亚洲欧洲一二三区 | 欧美一区二区网站 | 欧美激情性做爰免费视频 | 日韩特黄| 99久久这里只有精品 | 影音先锋激情 | 国产传媒国产传媒 | 亚洲成人自拍视频 | 亚州男人的天堂 | 欧美成人性生活 | 91精品播放 | 国产精品久久一区二区三区动 | 精品无码人妻少妇久久久久久 | 欧美做受高潮6 |