sasigiii 题目问答 · 2025/2/26
dfs里不重复,却不标记起点?
这道题很奇怪, 明明代码里不允许重复,但是却不标记起点,实例里面的方案也有回到起点的
0 0 0 15
1368330567 题目问答 · 2024/4/9
为啥跑样例就可以,评测不行?好像是超时
```cpp #include<bits/stdc++.h> using namespace std; char Map[1010][1010]; int n, m, ans; int d[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} }; struct point { int x; int y; int cnt; string str; }; void bfs(int x, int y) { queue<point> que;//重新构造队列 point start; start.x = x, start.y = y, start.cnt = 0; start.str = Map[x][y]; que.push(start); while (!que.empty()) { if (que.front().cnt == 6 && que.front().str == "tencent") { ans++; } else if (que.front().cnt > 6) break; for (int i = 0; i < 4; i++) { int tx = que.front().x + d[i][0], ty = que.front().y + d[i][1]; if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) { point temp; temp.x = tx; temp.y = ty; temp.cnt = que.front().cnt + 1; temp.str = que.front().str + Map[tx][ty]; que.push(temp); } } que.pop(); } } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> Map[i][j]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { bfs(i, j); } } cout << ans; return 0; } ```
查看全文
0 0 0 44