有一段代码:
read -p "Выбери число от 0 до 5: " $number
rand=$(($RANDOM%6))
if (( $(bc <<< "$rand -eq $nubmer") )); then
echo "Не повезло"
fi
变量值不同number
,rand
块if
仍然执行
这可能完全取决于数据类型转换,但我可能是错的
有一段代码:
read -p "Выбери число от 0 до 5: " $number
rand=$(($RANDOM%6))
if (( $(bc <<< "$rand -eq $nubmer") )); then
echo "Не повезло"
fi
变量值不同number
,rand
块if
仍然执行
这可能完全取决于数据类型转换,但我可能是错的
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector <vector<int>> g(n+1, vector<int> (m+1, 0));
vector <vector<int>> matrix(n+1, vector<int> (m+1, 0));
queue <pair<int, int>> q;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
{
cin >> g[i][j];
if(g[i][j] == 1) q.push({i, j});
}
vector <int> x = {1, -1, 0, 0};
vector <int> y = {0, 0, 1, -1};
while(!q.empty())
{
auto t = q.front();
q.pop();
int t_x = t.first, t_y = t.second;
for(int i = 0;i < 4;i++)
{
int xx = t_x + x[i], yy = t_y + y[i];
if(xx > 0 && xx <= n && yy > 0 && yy <= m && matrix[xx][yy] == 0)
{
matrix[xx][yy] = matrix[t_x][t_y] + 1;
q.push({xx, yy});
}
}
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
if(g[i][j] == 0)
cout << matrix[i][j] << " ";
else
cout << 0 << " ";
}
cout << endl;
}
}
该解决方案在某些测试中捕获了 WA,可能是什么错误?
为什么通过引用 DFS 传递数组会导致内存限制,但全局声明它们会通过所有测试?
错误解决方法:
#include <bits/stdc++.h>
using namespace std;
vector <int> res;
int n, m;
void dfs(int v, vector<vector<int>> graph, vector<int> &used, vector<pair<int, int>> edges)
{
used[v] = 1;
for(int u:graph[v])
{
if(used[u] == 0)
{
for(int j = 1;j <= m;j++)
{
pair<int, int> p1 = {u, v};
pair<int, int> p2 = {v, u};
if(edges[j] == p1 || edges[j] == p2){
res.push_back(j);
break;
}
}
dfs(u, graph, used, edges);
}
}
}
int main()
{
int ind = 1;
cin >> n >> m;
vector <vector<int>> graph(n+1);
vector <int> used(n+1, 0);
vector <pair<int, int>> edges(m+1);
for(int i = 0;i < m;i++)
{
int t1, t2;
cin >> t1 >> t2;
graph[t1].push_back(t2);
graph[t2].push_back(t1);
edges[ind] = {t1, t2};
ind++;
}
dfs(1, graph, used, edges);
cout << res.size() << endl;
for(int x:res) cout << x << " ";
}