Fox And Two Dots

本文介绍了一款名为TwoDots的游戏中的算法挑战,即如何在给定的游戏板上找到相同颜色的点构成的环形路径。通过深度优先搜索的方法实现这一目标,并给出了具体的实现代码。

点击打开链接

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:

  1. These k dots are different: if i ≠ j then di is different from dj.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1di 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).


解析:想要形成一个环必须能够从起点经过深度搜索回到起点,并且排出AA这种非环的情况。


#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 55;
int fx[4]={-1,1,0,0},fy[4]={0,0,-1,1};
int n,m,ok,vis[M][M],sx,sy;
char s[M][M];
void dfs(int x,int y,int flag)
{
	if(ok) return ;
	vis[x][y]=1;
	for(int i=0;i<4;i++){
		int xx=x+fx[i],yy=y+fy[i];
		if(xx==sx&&yy==sy&&flag>1){    //此处flag大于1或2都可以。 
			ok=1;return ;
		}
		if(xx>=0&&yy>=0&&xx<n&&yy<m&&!vis[xx][yy]&&s[xx][yy]==s[x][y]){
			dfs(xx,yy,flag+1);
        }
    }
}
int main()
{
	while(~scanf("%d%d",&n,&m)){
		ok=0;
		for(int i=0;i<n;i++)
		scanf("%s",s[i]);
		for(int i=0;i<n&&!ok;i++)
		for(int j=0;j<m&&!ok;j++){
			memset(vis,0,sizeof(vis));
			sx=i;sy=j;
			dfs(i,j,0);
		}
		if(!ok) puts("No");
		else puts("Yes");
	}
}


当 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 } // 后续处理 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值