Fox And Two Dots

本文介绍了一种使用深度优先搜索(DFS)算法来解决TwoDots游戏中寻找相同颜色圆点形成的有效循环的问题。通过避免回头路径确保了搜索的有效性,并提供了一个具体的实现示例。

题目来源于2017HPUACM暑期培训:https://vjudge.net/contest/174968#problem/D

Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:

Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.

The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:

    These k dots are different: if i ≠ j then di is different from dj.
    k is at least 4.
    All dots belong to the same color.
    For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.

Determine if there exists a cycle on the field.

Input

The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.

Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.

Output

Output "Yes" if there exists a cycle, and "No" otherwise.

Example
Input

3 4
AAAA
ABCA
AAAA

Output

Yes

Input

3 4
AAAA
ABCA
AADA

Output

No

Input

4 4
YYYR
BYBY
BBBY
BBBY

Output

Yes

Input

7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB

Output

Yes

Input

2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ

Output

No

Note

In first sample test all 'A' form a cycle.

In second sample there is no such cycle.

The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).

也是dfs的运用,但是注意这里面dfs不能回头,加变量判断它不走重复路线

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int n,m,k=0,vis[55][55],xx,yy;
char s[55][55];
int fx[4]={1,0,0,-1};
int fy[4]={0,-1,1,0};
void dfs(int x,int y,int px,int py,char ss)//px,py判断是否回头 
{
    if(k==1)
    {
        return;
    }
    for(int i=0;i<4;i++)
    {
        xx=x+fx[i];
        yy=y+fy[i];
        if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]==ss)
        {
            if(xx==px&&yy==py)//px,py判断是否回头
            {
                continue;
            }
            else
            {
                if(vis[xx][yy]&&s[xx][yy]==ss)
                {
                    k=1;
                    return ;
                }
            }
            vis[xx][yy]=1;
            dfs(xx,yy,x,y,s[xx][yy]);
        }
    }
}
int main()
{
    k=0;
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
       scanf("%s",s[i]);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!vis[i][j])
            {
                vis[i][j]=1;
                dfs(i,j,-1,-1,s[i][j]);
                if(k==1)
                  break;
            }
        }
        if(k==1)
          break;
    }
    if(k==1)
      printf("Yes\n");
    else
      printf("No\n");
return 0;
}
当 MineAdmin 提示未授权且出现 `The JWT string must have two dots` 错误,此错误表明 JWT 格式不合法,正常的 JWT 格式应为 `xxx.yyy.zzz`,即包含两个点。可以参考以下方法解决: ### 1. 检查 JWT 传递是否完整 在客户端传递 JWT 到服务端时,可能因网络问题或代码逻辑问题导致 JWT 字符串丢失部分内容。需要检查客户端代码,确保完整传递 JWT。例如在 JavaScript 中使用 `fetch` 发送请求时,要正确设置请求头携带 JWT: ```javascript const jwtToken = 'your_jwt_token'; fetch('your_api_url', { headers: { 'Authorization': `Bearer ${jwtToken}` } }) .then(response => response.json()) .then(data => console.log(data)); ``` ### 2. 检查服务端接收 JWT 是否正确 在服务端接收 JWT 时,可能因解析逻辑问题导致无法正确获取完整的 JWT 字符串。要检查服务端代码,确保正确从请求头中提取 JWT。以 PHP 为例: ```php $headers = getallheaders(); if (isset($headers['Authorization'])) { $jwt = str_replace('Bearer ', '', $headers['Authorization']); // 后续处理 JWT } ``` ### 3. 验证 JWT 生成逻辑 若 JWT 生成过程存在问题,也会导致生成的 JWT 格式不合法。要检查 JWT 生成代码,确保生成的 JWT 格式正确。参考引用中的 JWT 生成配置示例: ```php // 生成配置文件 php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" // 生成密钥 php artisan jwt:secret ``` 同时,确保模型类实现 `JWTSubject` 接口: ```php <?php namespace App; use Tymon\JWTAuth\Contracts\JWTSubject; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements JWTSubject { use Notifiable; public function getJWTIdentifier() { return $this->getKey(); } public function getJWTCustomClaims() { return []; } } ``` ### 4. 检查中间件或拦截器逻辑 部分中间件或拦截器可能会对 JWT 进行处理,如果处理逻辑有误,可能会破坏 JWT 的格式。要检查中间件或拦截器代码,确保不会意外修改 JWT 字符串。例如在 Laravel 中,可以检查自定义的 JWT 中间件: ```php <?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Tymon\JWTAuth\Facades\JWTAuth; class JwtMiddleware { public function handle(Request $request, Closure $next) { try { $user = JWTAuth::parseToken()->authenticate(); } catch (\Exception $e) { if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){ return response()->json(['status' => 'Token is Invalid']); }else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){ return response()->json(['status' => 'Token is Expired']); }else{ return response()->json(['status' => 'Authorization Token not found']); } } return $next($request); } } ``` ### 5. 调试 JWT 解析过程 可以在服务端添加调试信息,输出接收到的 JWT 字符串,检查其格式是否正确。参考引用中查看验证 JWT 的代码示例,在验证 JWT 格式时添加调试输出: ```go // Inspect a JWT func (j *JWT) Inspect(t string) (*auth.Account, error) { // simple validation should be of form xxx.yyy.zzz i.e. contain two dots fmt.Println("Received JWT:", t); // 添加调试输出 if len(strings.Split(t, ".")) != 3 { return nil, token.ErrInvalidToken } // 后续处理 } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三更鬼

谢谢老板!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值