神棍節獻禮之——POJ1111 Image Perimeters(bfs)

來源:互聯網
上載者:User

題很簡單,廣搜,深搜自己隨意就好。

BFS 代碼:

#include<cstdio>#include<cstring>#include<queue>using namespace std;#define N 31int n,m,mat[N][N],vis[N][N];int dir[8][2]={-1,0, 1,0, 0,-1, 0,1, -1,-1, -1,1, 1,-1, 1,1};struct node{int x,y;};int judge(int x,int y){if(x>=1 && y>=1 && x<=n && y<=m)return 1;return 0;}/*只有當搜到邊界時,才需要統計邊數!!!*/void bfs(int x,int y){queue<node> q;int i;memset(vis,0,sizeof(vis));node cur,next;cur.x=x; cur.y=y;vis[cur.x][cur.y]=1;q.push(cur);int sum=0;while(!q.empty()){cur=q.front();q.pop();for(i=0;i<8;i++){next.x=cur.x+dir[i][0];next.y=cur.y+dir[i][1];if(vis[next.x][next.y]==1)continue;if(judge(next.x,next.y) && mat[next.x][next.y]){//在界內 且 可達vis[next.x][next.y]=1;q.push(next);}else if(i<4)sum++;/*八個方向中,只有上下左右需要考慮邊界,剩下的四個方向的邊界對sum值沒有影響*/}}printf("%d\n",sum);}int main(){int i,j,x,y;char ch[2];while(scanf("%d%d%d%d",&n,&m,&x,&y),n){memset(mat,0,sizeof(mat));for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%1s",ch);if(ch[0]=='.')mat[i][j]=0;else mat[i][j]=1;}}if(mat[x][y]==0)puts("0");else bfs(x,y);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.