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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

中石油训练赛 - Bee Problem(dfs+连通块)

發(fā)布時(shí)間:2024/4/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 中石油训练赛 - Bee Problem(dfs+连通块) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

You are a busy little bee, and you have a problem. After collecting nectar all day long, you are returning to the beehive with a large supply of honey. You would really like to take a nap now, but sadly, you have to store all the honey in your beehive first. Opening up a cell in the hive to funnel honey into takes a lot of time, so you want to avoid doing this as much as possible.
Some of the cells in the beehive are already filled with old, hardened honey. The other cells are still empty. If you pour honey into an empty cell, it will automatically start flowing into adjacent empty cells. From these cells, the honey will again flow to other neighbouring empty cells. This saves you from having to funnel honey into them directly. You decide to use this to your advantage, by pouring into cells with lots of (indirect) adjacent open cells.

figure 1: The beehives from the first two samples. The black hexagons already contain hardened honey. The white cells are still empty.
You have some units of honey, and know the layout of your beehive. By cleverly choosing which cells to funnel honey into, what is the minimal amount of work you have to do?

?

輸入

? The input starts with three integers, 0 ≤ h ≤ 106, the amount of honey you have, and 1 ≤ n, m ≤ 103, the dimensions of the grid.
? Then follow n lines, one for each row of the grid. Each row has m symbols, either .,representing an empty cell, or #, representing a ?lled cell. These symbols are separated by spaces. Furthermore, every second row starts with a space, as these are slightly offset to the right.
The grid always has enough open cells to store all your honey.

?

輸出

Output a single integer, the number of cells you have to funnel honey into directly to store all your honey in the hive.

?

樣例輸入

復(fù)制樣例數(shù)據(jù)

8 4 4 . # # .# . # . # # # .# . . .

樣例輸出

3

題目鏈接:點(diǎn)擊查看


題目大意:給出一個(gè)h表示現(xiàn)在身上的蜂蜜數(shù)量,再給出一個(gè)蜂巢的結(jié)構(gòu)圖,黑色表示已經(jīng)滿了,白色表示是空著的,每個(gè)小格子互相都是連通的,我們可以將身上的蜂蜜往小格子中填充,問(wèn)若要將身上的蜂蜜都填充到蜂巢中,最少需要對(duì)幾個(gè)小格子操作

題目分析:emmmm,我知道我的語(yǔ)文很差,題意可能沒(méi)說(shuō)清楚,不過(guò)轉(zhuǎn)換一下,這就是一個(gè)讓求最大連通塊的問(wèn)題,只不過(guò)這個(gè)問(wèn)題有兩個(gè)方面需要注意一下,第一就是我們需要將所有的連通塊都求出來(lái),然后對(duì)每個(gè)連通塊進(jìn)行降序排序,因?yàn)檫@個(gè)題目中的dfs是邊搜邊更新的,所以每個(gè)點(diǎn)至多遍歷一次,時(shí)間復(fù)雜度也就控制在了n*m之內(nèi),這樣就不用擔(dān)心遞歸會(huì)超時(shí)的問(wèn)題了,第二個(gè)方面就是這個(gè)題目中,不像中規(guī)中矩的那種矩形一樣是上下左右走的,而是每個(gè)點(diǎn)有6個(gè)方向可以走,而奇數(shù)行和偶數(shù)行的方向又有那么一點(diǎn)點(diǎn)不同,所以我們一共設(shè)置12個(gè)方向,分別供奇數(shù)行和偶數(shù)行的點(diǎn)來(lái)使用,這個(gè)畫(huà)個(gè)圖就能推出來(lái)6個(gè)節(jié)點(diǎn)的編號(hào),這里不贅述了

然后還有一個(gè)小坑,就是在輸入的時(shí)候,題目每個(gè)節(jié)點(diǎn)之間都有一個(gè)空格,十分的惡心人,而且偶數(shù)行的開(kāi)頭還會(huì)有一個(gè)前置空格,我們?cè)谳斎氲臅r(shí)候要記著用getchar讀掉,或者可以直接用cin,可以略過(guò)空格直接讀到符號(hào),cin大法好嗷,還有一點(diǎn),這個(gè)題目多組輸入會(huì)TLE,我人當(dāng)時(shí)就傻了,可能還是我太菜了吧555

上代碼吧:

#include<iostream> #include<algorithm> using namespace std;const int N=1e3+100;char maze[N][N];int ans[N*N];int cnt;int h,n,m;const int b1[6][2]={0,1,0,-1,1,0,-1,0,-1,-1,1,-1};//jiconst int b2[6][2]={0,1,0,-1,1,0,-1,0,1,1,-1,1};//ouint num;void dfs(int x,int y) {num++;maze[x][y]='#';for(int k=0;k<6;k++){int xx,yy;if(x&1){xx=x+b2[k][0];yy=y+b2[k][1];}else{xx=x+b1[k][0];yy=y+b1[k][1];}if(xx<0||yy<0||xx>=n||yy>=m)continue;if(maze[xx][yy]=='#')continue;dfs(xx,yy);} }bool cmp(int a,int b) {return a>b; }int main() {scanf("%d%d%d",&h,&n,&m);cnt=0;getchar();for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>maze[i][j];for(int i=0;i<n;i++)for(int j=0;j<m;j++){if(maze[i][j]=='.'){num=0;dfs(i,j);ans[cnt++]=num;}}sort(ans,ans+cnt,cmp); // for(int i=0;i<cnt;i++) // cout<<ans[i]<<' '; // cout<<endl;int temp=0;int tem=0;int t=0;while(tem<h&&t<cnt){temp++;tem+=ans[t++];}cout<<temp<<endl;return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的中石油训练赛 - Bee Problem(dfs+连通块)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 关之琳三级全黄做爰在线观看 | 在线看日本 | 亚洲一区二区免费看 | 久久99一区 | 91看片黄色 | 69精品人妻一区二区三区 | 亚洲一区视频在线 | 国产一区二区三区免费观看视频 | 99热在线这里只有精品 | 91性高潮久久久久久久久 | 国产午夜啪啪 | 少妇特黄a一区二区三区88av | 国产二区精品 | 亚洲男人在线 | www.插插插.com | 天天操夜夜拍 | 手机av资源 | 亚洲成人一区二区三区 | www.99re.| 天天天综合网 | 欧美三级一区二区三区 | 91综合久久 | 97在线视频免费 | 日韩精品一区二区三区av | 在线国产中文字幕 | 免费看污片的网站 | 精品成人免费视频 | 色呦呦一区二区 | 免费看欧美黑人毛片 | 少妇高清精品毛片在线视频 | 久久久一级片 | 雷电将军和丘丘人繁衍后代视频 | 国产精品亚洲一区二区三区 | 亚一区二区 | 久久久噜噜噜久久 | 国产精品白丝喷水在线观看 | 日本网站在线看 | 青青国产在线观看 | 18禁免费无码无遮挡不卡网站 | 成人在线视频免费 | 久久无码性爱视频 | 在线观看三级网站 | 亚洲国产专区 | 国产又大又黄视频 | 91免费视频免费版 | 国产成人小视频在线观看 | 久久香蕉影院 | 在线免费看污片 | 亚洲在线影院 | 中文字幕15页 | 青草草在线 | 波多野吉衣在线视频 | 亚洲卡一卡二 | 99精品视频在线观看免费 | 伊人www| 亚洲精品偷拍 | 性色av一区二区三区免费 | www.av网| 久久精品九九 | 99热这里只有精品5 国产精品伦子伦免费视频 精品一二三 | 囯产精品一品二区三区 | 天天舔天天操天天干 | 久久久一二三四 | 亚洲影视一区二区 | 欧洲中文字幕 | 超91在线 | 国产婷 | 国产老女人乱淫免费 | 狠狠激情| 伊人热久久| 欧美精品一二三 | 欧美日韩h | 18在线观看免费入口 | 亚洲黄色小说网 | 男女超爽视频免费播放 | 欧州一区| 1024日韩| 2019国产精品视频 | 99福利网 | 裸体毛片 | 17c在线视频 | 在线观看的黄网 | 久久久综合网 | 新婚夫妇白天啪啪自拍 | 免费黄色在线播放 | 三级视频久久 | 国产图片一区 | 国产成人av无码精品 | 人妻 丝袜美腿 中文字幕 | 成人av免费在线看 | 国产精品午夜影院 | 午夜在线观看av | 91片黄在线观看 | 一级黄网| 免费午夜av | 亚洲精品免费在线观看 | 色综合久久中文字幕无码 | 一边摸一边抽搐一进一出视频 | 日本四级电影 |