In-depth prioritization algorithms

Source: Internet
Author: User

Today we use an in-depth search algorithm to find out about a puddle problem. For a long time did not take a serious look at C + + books, in this issue found themselves still a lot of places unfamiliar.

For example, the transfer of arrays in C + + should be writtenvoid dfs(int x,int y,char(&field) [N] [M ]) where [] the priority is higher than &, so the previous pass-through value is not.
  1. #include <iostream>using namespace std; #define N 10#define M 12//The first step is to set the current place to '. ', followed by checking all the other places to see if it meets the requirements, And make a recursive call to void Dfs (int x,int y,char (&field) [n][m]) {field[x][y] = '. '; for (int i =-1, i < 2; ++i) {for (int j =-1; j < 2; ++j) {int nx=x+i,ny=y+j;if (0&LT;=NX && NX <n &AMP;&A mp 0<=ny && ny<m && (field[nx][ny] = = ' W ') {DFS (Nx,ny,field);}}}} int main (int argc, char const *argv[]) {char Field[n][m] = {{' W ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' w ', '. '},{'. ' ' W ', ' W ', '. ', '. ', '. ', '. ', '. ', ' W ', ' W ', ' W '},{'. ', '. ', '. ', '. ', ' W ', ' W ', '. ', '. ', '. ', ' W ', ' W ', '. '},{'. ', '. ', '. ', '. ', '. ' , '. ', '. ', '. ', '. ', ' W ', '. ', '. '},{'. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' . ', '. ', '. ', ' w ', '. '},{' W ', ' W ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' W ', ' W ', '. '},{', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ' , '. ', ' W '},{' W ', ' W ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' w ', '. '},{', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. '} ; int res = 0;for (int i = 0; i < N; ++i) {for (int j = 0; J < M; ++J) {if (field[i][j]== ' W ') {DFS (i,j,field); res++;}}} cout<<res<<endl;cout<< "GFs is over!" <<endl;return 0;}

      

Another way to do this is to declare the field array as a global variable:externchar field[N] [M]
  1. #include <iostream>using namespace std; #define N 10#define M 12extern char field[n][m] = {{' W ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' W ', ' W ', '. '},{'. ', ' W ', ' W ', ' W ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' W ', ' W ', ' W '},{'. ', '. ', '. ', '. ', ' W ', ' W ', '. ' , ' W ', ' W ', '. '},{'. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' w ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ', '. ' . '},{', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' w ', '. '},{' W ', ' W ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '},{' w ', ' . ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', ' W '},{' W ', ' W ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '},{', '. ', '. ' , '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '}};//', '. ', first the first step is to set the current place as '. [Y] = '. '; for (int i =-1, i < 2; ++i) {for (int j =-1; j < 2; ++j) {int nx=x+i,ny=y+j;if (0&LT;=NX && NX <n &AMP;&A mp 0<=ny && ny<m && (field[nx][ny] = = ' W ') {DFS (nx,ny);}}}} int main (int argc, char const *argv[]) {int res = 0;for (int i = 0; i < N; ++i) {for (int j = 0; J < M; + +)j) {if (field[i][j]== ' W ') {DFS (i,j); res++;}}} cout<<res<<endl;cout<< "GFs is over!" <<endl;return 0;}

      


Today is the first day to really start thinking about the knowledge of the algorithm, now in the algorithm is still belong to the super small white stage, but good refueling, I believe that one day will be more excellent!



From for notes (Wiz)

In-depth prioritization algorithms

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.