FOJ 1408 位元影像

來源:互聯網
上載者:User

此題還是BFS的思想。

對每個為1的點進行BFS.

若BFS的值小於原來的,則進行替換。

這種題目注意格式問題了。。。

不要多個空格。。。。

 

#include <iostream><br />#include <queue><br />using namespace std;<br />#define X 200<br />#define Y 200</p><p>struct Point_Struct<br />{<br />int x;<br />int y;<br />int flag;<br />};</p><p>int BitMap[X+2][Y+2];</p><p>void BFS(int x,int y)<br />{<br />queue<Point_Struct> Queue;<br />Point_Struct Temp_Point,Temp_Point2;<br />int plusX[] = {0,1,0,-1};<br />int plusY[] = {1,0,-1,0};<br />int i;<br />Temp_Point.x = x;<br />Temp_Point.y = y;<br />Temp_Point.flag = 0;<br />Queue.push(Temp_Point);<br />while (!Queue.empty())<br />{<br />Temp_Point = Queue.front();<br />for (i = 0;i<4;i++)<br />{<br />if (BitMap[Temp_Point.x+plusX[i]][Temp_Point.y+plusY[i]]==-1||<br />BitMap[Temp_Point.x+plusX[i]][Temp_Point.y+plusY[i]]>Temp_Point.flag+1)<br />{<br />BitMap[Temp_Point.x+plusX[i]][Temp_Point.y+plusY[i]] = Temp_Point.flag+1;<br />Temp_Point2.x = Temp_Point.x+plusX[i];<br />Temp_Point2.y = Temp_Point.y+plusY[i];<br />Temp_Point2.flag = Temp_Point.flag+1;<br />Queue.push(Temp_Point2);<br />}<br />}<br />Queue.pop();<br />}<br />}</p><p>int main()<br />{<br />int x,y;<br />int i,j;<br />char str[Y+1];<br />while (scanf("%d%d", &x,&y)!=EOF)<br />{<br />memset(BitMap, 0, sizeof(BitMap));<br />for (i=0;i<=y+1;i++)<br />{<br />BitMap[0][i] = -2;<br />BitMap[x+1][i] = -2;<br />}<br />for (i=0;i<=x+1;i++)<br />{<br />BitMap[i][0] = -2;<br />BitMap[i][y+1] = -2;<br />}<br />for (i=1;i<=x;i++)<br />{<br />scanf("%s", str);<br />for (j=1;j<=y;j++)<br />BitMap[i][j] = str[j-1]-'0'-1;<br />}</p><p>for (i=1;i<=x;i++)<br />{<br />for (j=1;j<=y;j++)<br />if (BitMap[i][j] == 0)<br />BFS(i,j);<br />}</p><p>for (i=1;i<=x;i++)<br />{<br />for (j=1;j<y;j++)<br />printf("%d ", BitMap[i][j]);<br />printf("%d/n",BitMap[i][j]);<br />}<br />}<br />return 0;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.