【DFS】两道上下左右走的dfs

BabbleDay posted @ 2013年7月20日 03:55 in 刷题防身 , 707 阅读

这是在理解dfs大致意思之后自己写的,一定是不够优雅的,但是觉得很开心~所以刚着手dfs的童鞋也可以试试~

hdu 1312 题目大意:在'#'和'.'的矩阵中找有多少个点是相连的

#include <iostream>
#include <cstring>
#define N 22
using namespace std;

int cnt, w, h, sx, sy;
char input[N][N];
bool visited[N][N];
void dfs(int y, int x) // 这里四个for循环是上下左右不同方向试,可以用一个dir数组和一个for来代替,详见下例
{
    if(y-1>0 && input[y-1][x]=='.' && !visited[y-1][x])
    {
        cnt ++;
        visited[y-1][x] = 1;
        dfs(y-1,x); 
    }
    if(y+1<=h && input[y+1][x]=='.' && !visited[y+1][x])
    {
        cnt ++;
        visited[y+1][x] = 1;
        dfs(y+1,x); 
    }
    if(x-1>0 && input[y][x-1]=='.' && !visited[y][x-1])
    {
        cnt ++;
        visited[y][x-1] = 1;
        dfs(y,x-1); 
    }
    if(x+1<=w && input[y][x+1]=='.' && !visited[y][x+1])
    {
        cnt ++;
        visited[y][x+1] = 1;
        dfs(y,x+1); 
    }
    return;
}
int main()
{
    while(cin >> w >> h, w | h)
    {
        memset(input, 0, sizeof(input));
        memset(visited, 0, sizeof(visited));
        cnt = 0;
        for(int i=1; i<=h; i++)
            for(int j=1; j<=w; j++)
            {
                cin >> input[i][j];
                if(input[i][j]=='@')
                {
                    sx = j;
                    sy = i;
                }
            }
        cnt ++;
        visited[sy][sx] = 1;
        dfs(sy, sx);
        cout << cnt << endl;
    }
    return 0;
}

hdu 1241 题目大意:在‘*’和‘@’的矩阵中求有几块‘@’是连在一起的

#include <iostream>
#include <cstring>
#include <cstdio>
#define N 105
using namespace std;

int dir[8][2] = { {1,0}, {-1,0}, {0,-1}, {0,1}, {1,-1}, {1,1}, {-1,-1}, {-1,1} };//其实只要最外面的大括号就可以了
int m, n, cnt;
char input[N][N];
bool visited[N][N];

void dfs(int i, int j)
{
    //cout << "dfs" << i << j << "  ";
    if(visited[i][j] || i<1 || i>m || j<1 || j>n) return;
    visited[i][j] = true;
    if(input[i][j]=='*') return;
    for(int k=0; k<8; k++)
    {
        dfs(i+dir[k][0], j+dir[k][1]);
    }
}
int main()
{
    while(scanf("%d %d", &m, &n) &&  m|n)
    {
        //cout << m << n << endl;
        memset(input, 0, sizeof(input));
        memset(visited, 0, sizeof(visited));
        cnt =0 ;
        for(int i=1; i<=m; i++)
            for(int j=1; j<=n; j++)
                cin >> input[i][j];
        for(int i=1; i<=m; i++)
            for(int j=1; j<=n; j++)
            {
                if(!visited[i][j])
                {
                //cout << "visit" << i << " " << j << ": ";
                    //cout << input[i][j] << endl;
                    if(input[i][j]=='@') 
                    {
                        cnt++;
                        //cout << cnt << endl;
                        dfs(i, j);
                    }
                    else visited[i][j] = true;
                }
            }
        cout << cnt << endl;
    }
    return 0;
}
Avatar_small
photoshop generative 说:
2023年7月21日 05:00

Adobe Photoshop hat kürzlich ein neues Tool namens Generative Fill veröffentlicht, das die KI-Technologie Firefly nutzt, um Benutzern dabei zu helfen, ihre Fotos auf aufregende Weise zu verbessern. photoshop generative fill aktivieren Dieses Tool ermöglicht Benutzern das einfache Erweitern von Fotos, das Hinzufügen oder Entfernen von Objekten mithilfe einfacher Textaufforderungen und bietet im Vergleich zu anderen Tools wie Content-Aware Fill mehr Kontrolle.

Avatar_small
NCERT 10th Class Re 说:
2023年9月01日 22:45

NCERT Class 10th new Syllabus 2024 is Recently Released by National Council of Educational Research and Training (NCERT) is an Autonomous Organisation set up in by the Government of India to Assist and Advise the Central and State Governments on Policies and Programmes for Qualitative Improvement in School Education,Students are Advised to Thoroughly go Through the NCERT Class Syllabus before they NCERT 10th Class Revised Syllabus 2024 Start Studying for the new Session 2024.NCERT Class Syllabus 2024 Pdf Download.NCERT Class Examination Every Year Conducted Month of March, In this Web page you will get the Complete Syllabus of Accountancy.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter