uva10422 bfs+哈希判重 TLE

调了半天,还是TLE,看了网上的代码基本没有用bfs+哈希判重做的,要么是dfs要么压根不用哈希判重,难道说bfs+哈希注定过不了?

看了下程序只有算哈希函数的地方有一个25的循环比较累赘,别的地方循环不是很多,可是25位必须要循环才能算哈希函数额。。。

求解释。

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

const int MAX=60003;
int n,sx,sy,count,head[MAX],Next[MAX],que[MAX];
int dx[8]={-1,-2,-2,-1,1,2,2,1};
int dy[8]={-2,-1,1,2,2,1,-1,-2};
char chess[6][6],vis[MAX][26];
char target[26]="111110111100 110000100000";

int calhash(char arr[])
{
	int s=0;
	for (int i=0;i<25;i++)
		s=s*10+arr[i];
	return (s & 0x7FFFFFFF)%MAX;		//ensure s is positive
}
bool hasvisited(int s)
{
	int h=calhash(vis[s]);
	int u=head[h];
	while (u)
	{
		if (!strcmp(vis[u],vis[s]))
			return true;
		u=Next[u];
	}
	Next[s]=head[h];
	head[h]=s;
	return false;
}

void toArray(int s)
{
	int p=0;
	for (int i=0;i<5;i++)
		for (int j=0;j<5;j++)
			vis[s][p++]=chess[i][j];
	vis[s][p]='\0';
}

void bfs()
{
	int front=1,rear=2;
	que[0]=0;
	que[1]=sy*5+sx;
	toArray(1);
	count=-1;
	bool flag=0;
	hasvisited(1);
	while(front<rear)
	{
		count++;
		if (count==10)
		{
			cout<<"Unsolvable in less than 11 move(s)."<<endl;
			break;
		}
		int tmprear=rear;
		while(front<tmprear)
		{
			if (!strcmp(vis[front],target))
			{
				cout<<"Solvable in "<<count<<" move(s)."<<endl;
				flag=1;
				break;
			}
			int loc=que[front];
			int ox=loc%5;
			int oy=loc/5;
			for (int i=0;i<8;i++)
			{
				int nx=ox+dx[i];
				int ny=oy+dy[i];
				if (nx>=0&&nx<5&&ny>=0&&ny<5)
				{
					int nloc=ny*5+nx;
					memcpy(vis[rear],vis[front],sizeof(vis[front]));
					char tmpchar=vis[rear][loc];
					vis[rear][loc]=vis[rear][nloc];
					vis[rear][nloc]=tmpchar;
					if (!hasvisited(rear))
					{
						que[rear++]=nloc;
					}
				}
			}
			front++;
		}
		if (flag) break;
	}
}
int main()
{
	cin>>n;
	getchar();
	while (n--)
	{
		for (int i=0;i<5;i++)
		{
			cin.getline(chess[i],6);
			for (int j=0;j<5;j++)
			{
				if (chess[i][j]==' ') {sx=j;sy=i;}
			}
		}
		memset(head,0,sizeof(head));
		memset(vis,0,sizeof(vis));
		bfs();
	}
	return 0;
}


### C++ 中使用 BFS 和 For 循环替代 DFS 的实现 在解决网格类问题时,广度优先搜索 (BFS) 可作为深度优先搜索 (DFS) 的有效替代方案。对于岛屿等问题而言,可以利用队列来管理待访问的位置,并通过 `for` 循环迭代处理这些位置。 #### 岛屿数量问题的 BFS 解决方案 下面是一个基于 BFS 来计算岛屿数量的例子: ```cpp #include <vector> #include <queue> using namespace std; class Solution { public: int numIslands(vector<vector<char>>& grid) { if (grid.empty() || grid[0].empty()) return 0; int rows = grid.size(); int cols = grid[0].size(); int islands = 0; vector<pair<int, int>> directions{{0,-1},{-1,0},{0,1},{1,0}}; for (int r = 0; r < rows; ++r){ for (int c = 0; c < cols; ++c){ if (grid[r][c] == '1'){ ++islands; queue<pair<int, int>> q; q.push({r,c}); while (!q.empty()){ auto [row, col] = q.front(); q.pop(); // 如果当前位置已经被标记,则跳过 if(grid[row][col]=='0') continue; // 将当前陆地标记为已访问 grid[row][col]='0'; // 对四个方向上的相邻节点进行探索 for(auto& dir : directions){ int newRow = row + dir.first; int newCol = col + dir.second; // 检查边界条件以及是否是未访问过的土地 if(newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == '1') q.push({newRow,newCol}); } } } } } return islands; } }; ``` 此代码片段展示了如何使用 BFS 方法遍历整个地图并统计岛屿的数量。每当遇到一个新的岛屿部分(即值为 `'1'`),就启动一次新的 BFS 查找过程直到该岛完全被淹没为止。在此过程中,所有属于同一座岛屿的部分都会被设置成水 (`'0'`) 以防止复计数[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值