UVa 10946 Do You What Filled? (DFS)

本文介绍了一个基于深度优先搜索(DFS)算法的应用实例,通过搜索二维矩阵中相连字符的数量来解决问题。文章提供了完整的C++实现代码,并对代码进行了注释说明,帮助读者理解DFS的基本原理及其在实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很显然,数据这么小,dfs必然会过的 

按上下左右搜,然后搜过的标记一下,避免重复,就哦啦

代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 55;
int n, m, id;
char g[N][N];
bool vis[N][N];
struct node {
    char x;
    int num;
}ans[3000];
void dfs( int u, int v ) {
    vis[u][v] = true;
    ans[id].num++;
    if ( u > 0 && g[u][v] == g[u-1][v] && !vis[u-1][v] ) dfs( u-1, v );
    if ( u < n-1 && g[u][v] == g[u+1][v] && !vis[u+1][v] ) dfs( u+1, v);
    if ( v > 0 && g[u][v-1] == g[u][v] && !vis[u][v-1] ) dfs( u, v-1 );
    if ( v < m-1 && g[u][v] == g[u][v+1] && !vis[u][v+1] ) dfs( u, v+1 );
}
bool cmp ( node a, node b ) { 
    if ( a.num == b.num ) return a.x < b.x;
    return a.num > b.num; 
}
int main()
{
    int pro = 1;
    while ( scanf("%d%d", &n, &m) != EOF && !( !m && !n ) ) {
        id = 0;
        getchar();
        for ( int i = 0; i < n; getchar(), ++i )
            for ( int j = 0; j < m; ++j ) scanf("%c", &g[i][j]);
        memset( vis, 0, sizeof(vis));
        for ( int i = 0; i < n; ++i )
            for ( int j = 0; j < m; ++j ) 
                if ( g[i][j] != '.' && !vis[i][j] ) {
                    ans[id].x = g[i][j];
                    ans[id].num = 0;
                    dfs( i, j ); id++;
                }
        sort( ans, ans + id, cmp );
        printf("Problem %d:\n", pro++);
        for ( int i = 0; i < id; ++i ) printf("%c %d\n", ans[i].x, ans[i].num);
    }
}





Sure, here's an example of a basic login page: HTML code: ``` <!DOCTYPE html> <html> <head> <title>Login Page</title> </head> <body> <h1>Login Page</h1> <form action="login.php" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username" required><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" required><br><br> <input type="submit" value="Login"> </form> </body> </html> ``` Explanation: - The `<!DOCTYPE html>` declaration at the beginning tells the browser that this is an HTML5 document. - The `<html>` element is the root element of the page. - The `<head>` element contains metadata about the page, such as the title. - The `<title>` element sets the title of the page, which appears in the browser's title bar or tab. - The `<body>` element contains the visible content of the page. - The `<h1>` element is a heading that displays "Login Page" at the top of the page. - The `<form>` element is used to create a form for user input. - The `action` attribute of the form specifies the URL of the script that will handle the form data. In this case, it's "login.php". - The `method` attribute of the form specifies the HTTP method to use when sending the form data. In this case, it's "post". - The `<label>` elements provide a text label for the corresponding form input element. - The `for` attribute of the `<label>` element should match the `id` attribute of the corresponding form input element. - The `<input>` elements are form controls that allow the user to enter data. The `type` attribute specifies the type of input, such as "text" for a text box or "password" for a password field. - The `id` and `name` attributes of the `<input>` elements are used to identify the form data when it's sent to the server. - The `required` attribute of the `<input>` elements tells the browser that the field must be filled in before the form can be submitted. - The final `<input>` element is a "submit" button that the user can click to submit the form. Assuming you have a PHP script called "login.php" that handles the form data, here's an example of what that script might look like: ``` <?php $username = $_POST["username"]; $password = $_POST["password"]; // TODO: Validate the username and password here if ($username == "admin" && $password == "password") { // The login is successful, so redirect to the home page header("Location: home.php"); exit(); } else { // The login is not successful, so display an error message echo "Invalid username or password"; } ?> ``` Explanation: - The `$_POST` superglobal array is used to retrieve the form data that was submitted. - The `$username` and `$password` variables contain the values that the user entered. - You should validate the username and password here to make sure they meet your requirements. For example, you might check that the username is a valid email address and that the password contains at least one digit and one uppercase letter. - If the login is successful (in this example, we're checking for a hardcoded username and password), you can redirect the user to the home page using the `header` function. Note that you must call `exit()` after the `header` function to prevent further execution of the script. - If the login is not successful, you can display an error message using the `echo` statement. You might also want to include a link back to the login page so the user can try again.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值