bfs beginner

BFS:

** Know initial and target state, with two-way BFS:
no right to use the best view of BFS
do not repeat as team **

Implementation Framework:

The copied (source: https://www.luogu.org/blog/stephen2333/solution-p1135 )
PS: I also wrote a start, but really think he writes good

  1. For the initial state into the team, set the initial state as visited
  2. If the queue is not empty, the teams head element, otherwise skip to step 5
  3. Check that the team for the final element of the solution, if it is then skip to step 5.
  4. For a team element, check all the neighboring states, if valid and is not accessible, then
    all available neighboring states into the team, and set these states as visited, then
  5. Skip to Step 2. Repeat the inspection team of the last element is the ultimate solution, if it is output, or no solution description

Typically implemented with queues (first in first out, FIFO)

初始化队列q;
q = {起点u};标记s为已访问;
while(q为非空){
    去q队首元素 now;now出队;
    If(now == 目标状态) {….}
    所有与now 相连 并 且未被访问的点进入队列(没被标记);
    并标记now已访问;
}

Search width of the rectangle:

Description Title
a rectangular array of numbers consisting of 0 to 9, the numbers 1 to 9 denotes a cell, the cell is approximately defined along the upper and lower cell number when compared with the same cell or cell number, given the number of required cells in a rectangular array. ? (1 <= m, n <= 100)
input and output format of
the input format:

Input: integers m, n (m rows, n columns)
matrix
output format:

Output: The number of cells
/
. 4 10
0. 5 0. 4 0 2 0. 3. 6. 7
. 1 0. 5. 4. 3. 5 0 0. 6 0
2. 4. 5. 6 0 0 0. 6. 7. 1
0 0 0 0 0 0 0 0. 8. 9
/

#include<cstdio>
#define maxn 100

int m,n,cnt = 0;
char s[maxn];
int a[maxn][maxn],vis[maxn][maxn];

int q[maxn];
int movex[5] = {0,1,-1,0,0};
int movey[5] = {0,0,0,1,-1};

void bfs(int x, int y) {
    int l = 0, r = 0;
    q[r++] = x;
    q[r++] = y;//按顺序加入  取出时就是按的这个顺序取出
    while(l < r) {//当 l = r时 队列为空  就找完第53行传入的点附近的数(可以和它构成cells的) 
        int nowx = q[l++];
        int nowy = q[l++];//取出当前点的位置
        for(int i = 1; i <= 4; i++) {//判断四个方向(上下左右)的数  
            int ix = nowx + movex[i];//当前(四个之一)方向上的坐标
            int iy = nowy + movey[i];
            if(ix < 1 || iy < 1 || ix > n || iy > m) continue;//一定要写!!!矩阵的bfs的边界 
            //if(ix == 0 || iy == 0 || ix || ix > n || iy > m)  continue;//最好写上面的 
            if(a[ix][iy]) {
                if(!vis[ix][iy]) {//再次判断它用不用被选 选过之后就没必要选了 
                    vis[ix][iy] = 1;
                    q[r++] = ix;
                    q[r++] = iy;//入队 
                } 
            }
        }
    }
}

int main() {
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) {
        scanf("%s",s);//先输入一行较长的字符数组,因为原题的没有空格 
        for(int j = 1; j <= m; j++) {
            a[i][j] = s[j - 1] - '0';//s[j - 1]是s的第一个元素 
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(!vis[i][j]) {//找出没有走过的点
                vis[i][j] = 1;//做标记
                if(a[i][j]) { //这才是找到可以宽搜的 点
                    bfs(i,j);
                    cnt++;
                }
            }
        }
    }
    printf("%d",cnt);
}

DFS;

Usually when seized combination, the optimal solution, we consider the use of DFS

Data structures: the system stack;

Guess you like

Origin www.cnblogs.com/virtualtan/p/10972470.html