1、易错点:一个点可能有很多的传送门,因此我们用vector来讲该坐标和该坐标的传送门一一对应
struct xb
{
int x, y;
}st, ed;vector
v[N][N];
2、关于传送门,其出口可能为陷阱
3、然后这题与一般的走迷宫的区别在于多了个传送门,因此我们用优先队列,对于传送门的步数,就为传送门入口的步数+3,然后该队列按照步数从小到大排序,并且用vis数组来看该点是否有到过,这样就可以避免同一个点不同的步数
- # include
- using namespace std;
- const int N = 3e2+5;
- struct xb
- {
- int x, y;
- }st, ed;
- struct Q{
- int x, y, step;
- bool operator < (const Q &s) const{
- return step>s.step;
- }
- };
- vector
v[N][N]; - char s[N][N];
- bool vis[N][N];
- priority_queue
q;
- int n, m, t;
- int X[4] = {0, 0, 1, -1};
- int Y[4] = {1, -1, 0, 0};
- int bfs()
- {
- while (!q.empty())
- {
- auto T = q.top(); q.pop();
- int x = T.x, y = T.y, st = T.step;
- if (vis[x][y] || x<1||x>n||y<1||y>m||s[x][y] == '#') continue;
- if (x == ed.x && y == ed.y)
- return st;
- vis[x][y] = true;
- for (int i=0; i<4; i++) q.push({x+X[i], y+Y[i], st+1});
- for (auto u:v[x][y]) q.push({u.x, u.y, st+3});
- }
- return -1;
- }
- int main()
- {
- while (cin>>n>>m>>t)
- {
- while (!q.empty())
- q.pop();
- for (int i=1; i<=n; i++)
- {
- for (int j=1; j<=m; j++)
- {
- cin>>s[i][j];
- vis[i][j] = false;
- v[i][j].clear();
- if (s[i][j] == 'S')
- st = {i, j};
- if (s[i][j] == 'T')
- ed = {i, j};
- }
- }
- q.push({st.x, st.y, 0});
- for (int i=1; i<=t; i++)
- {
- int x1, x2, y1, y2;
- cin>>x1>>y1>>x2>>y2;
- x1++, y1++, x2++, y2++;
- v[x1][y1].push_back({x2, y2});
- }
- for (auto u:v[st.x][st.y]) q.push({u.x, u.y, 3});
- cout<<bfs()<
- }
-
-
- return 0;
- }
NC50243 小木棍
题目链接
关键点:
1、首先明确枚举原先的小木棍长度,题目要求求最短的原先木棍长度,我们从可能最小的木棍长度从小到大枚举,遇到可行的就输出
2、将木棍长度排序,然后求和,从第一个数组开始枚举到所有的和
3、剪枝:(一)我们从大木棍开始排,(二)遇到拿过的木棍就跳过,(三)遇到当前木棍大于剩余长度跳过,(四)遇到当前木棍拼不上,且当前木棍为最后一个(一个长木棍)或者第一个(一个长木棍)时,那么后面的木棍肯定都不行,直接返回false,(五)然后对于当前木棍拼不上的,那么后面和他相同长度的也可以直接跳过
4、还有一步就是在枚举长木棍长度时,对于长度i,要保证
sum%i==0
这样才有可能由小木棍拼成
- # include
- using namespace std;
- int n, minn = 100, sum;
- int a[100];
- int vis[100];
- bool dfs(int num, int len, int rest, int now)
- {
- if (num==0&&rest==0) return true;
- if (rest==0) rest = len, now = 1;
- for (int i=now; i<=n; i++)
- {
- if (vis[i]) continue;
- if (a[i]>rest) continue;
- vis[i] = 1;
- if (dfs(num-1, len, rest-a[i], i)) return true;
- vis[i] = 0;
- if (a[i] == rest || len==rest) break;
- while (a[i]==a[i+1])
- i++;
- }
- return false;
- }
- bool cmp(int x, int y)
- {
- return x>y;
- }
- int main()
- {
- cin>>n;
- for (int i=1; i<=n; i++)
- {
- cin>>a[i];
- sum += a[i];
- }
- sort(a+1, a+1+n, cmp);
- for (int i=a[1]; i<=sum; i++)
- {
- memset(vis, 0, sizeof(vis));
- if (sum%i!=0) continue;
- if (dfs(n, i, i, 1))
- {
- cout<
- break;
- }
- }
-
-
- return 0;
- }
-
相关阅读:
YOLOv8 快速入门
基于electron+vue+element构建项目模板之【自定义标题栏&右键菜单项篇】
unity的乐高世界
协同细菌觅食优化算法(Matlab代码实现)
C语言--每日五道练习题--Day16
【idea】解决idea 执行maven build总下载 Downloading maven-metadata.xml文件
【LeetCode刷题笔记】一维数组
创建型模式-抽象工厂模式
Java中的正则表达式是什么该怎么用?
ValueError: Expected 2D array, got 1D array instead:
-
原文地址:https://blog.csdn.net/m0_60531106/article/details/126475155