Treasure Island Adventure

Source: Internet
Author: User

Xiao Hum gets an incomplete map of the Diaoyu aerial photos by secret means. The fish island consists of a main island and some affiliated islands
Xiangyu composition, small hum decided to go to the Diaoyu Island risk. The two-dimensional matrix of the 10* 1 0 below is the aerial map of the Diaoyu Islands. In the figure
The numbers indicate the elevation, 0 means the ocean, and the i~9 all represent land. The little hum plane will be landed at (6,8), now need to count
Figure out the area (i.e. how many squares) of the island where the small hum-down clan is located. Note Here we put the little hum down the landing point around

The linked land is considered to be an island.

After figuring out the problem. You will find in fact the breadth first search from (6,8). Every time you need to move up or down four
extension, and when the expanded point is greater than 0 o'clock, the queue is queued until the queue expands. All that are added to the queue
The total number of points is the area of the islets. Assume that the size of the map does not exceed 50*50. The code is implemented as follows.


Wide Search Implementation code:

#include <stdio.h>struct node{int x;int y;}; int main (void) {struct node que[2501];int head,tail;int a[51][51];int book[51][51]={0};int i,j,k,sum,max=0,mx,my,n,m, Startx,starty,tx,ty;int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};scanf ("%d%d%d%d",&n,&m,&startx,& Starty); for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {scanf ("%d", &a[i][j]);}} Head=1;tail=1;que[tail].x=startx;que[tail].y=starty;tail++;book[starty][starty]=1;sum=1;while (Head<tail) { for (k=0;k<=3;k++) {tx=que[head].x+next[k][0];ty=que[head].y+next[k][1];if (tx<1| | tx>n| | ty<1| | Ty>n)  continue;  if (a[tx][ty]>0&&book[tx][ty]==0)  {  sum++;  Book[tx][ty]=1;  QUE[TAIL].X=TX;  Que[tail].y=ty;  tail++;}  } head++;} printf ("%d\n", sum); return 0;}
The deep search code is as follows:

#include <stdio.h>int a[51][51];int book[51][51],n,m,sum;void dfs (int x,int y) {int next[4][2]={{0,1},{1,0},{0,- 1},{-1,0}};int k,tx,ty;for (k=0;k<=3;k++) {tx=x+next[k][0];ty=y+next[k][1];if (tx<1| | tx>n| | ty<1| | TY>M)   continue;if (a[tx][ty]>0&&book[tx][ty]==0) {Sum++;book[tx][ty]=1;dfs (tx,ty);}} return;} int main (void) {int i,j,startx,starty;scanf ("%d%d%d%d", &n,&m,&startx,&starty); for (i=1;i<=n;i++ {for (j=1;j<=m;j++) {scanf ("%d", &a[i][j]);}} Book[startx][starty]=1;sum=1;dfs (Startx,starty);p rintf ("%d\n", sum); return 0;}



Treasure Island Adventure

Related Article

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.