POJ-1111 Image Perimeters

Source: Internet
Author: User

Q: Ask For the circumference of 'X'

Idea: it is reasonable to add a perimeter plus 4, but to subtract duplicates, here I am using BFS. If it is a BFS template, it won't work, it should be removed first and then marked

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;const int MAXN = 30;struct node {int x,y;};queue
     
       qu;int dx[] = {0,0,1,-1,1,1,-1,-1};int dy[] = {1,-1,0,0,1,-1,1,-1};int vis[MAXN][MAXN],R,C,sx,sy,ans;char map[MAXN][MAXN];int check(int x, int y) {if (x >= 0 && x < R && y >= 0 && y < C && map[x][y] == 'X')return 1;return 0;}int bfs(int r, int c) {node st;st.x = r,st.y = c;qu.push(st);while (!qu.empty()) {node cur = qu.front();qu.pop();if (!vis[cur.x][cur.y]) {ans += 4;for (int i = 0; i < 4; i++) {int nx = cur.x + dx[i];int ny = cur.y + dy[i];if (check(nx, ny) && vis[nx][ny])ans -= 2;}vis[cur.x][cur.y] = 1;for (int i = 0; i < 8; i++) {int nx = cur.x + dx[i];int ny = cur.y + dy[i];if (check(nx, ny)) {st.x = nx;st.y = ny;qu.push(st);}}}}return ans;}int main() {while (scanf("%d%d%d%d%*c", &R, &C, &sx, &sy) != EOF && R+C+sx+sy != 0) {memset(map, 0, sizeof(map));memset(vis, 0, sizeof(vis));while (!qu.empty()) qu.pop();ans = 0;for (int i = 0; i < R; i++)gets(map[i]);printf("%d\n", bfs(sx-1, sy-1));}return 0;}
     
    
   
  
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.