我正在使用cout
并endl
打印一些指南,输出(我的意图)如下:
From Spot 1
Spot 2
Spot 5
Spot 8
Spot 6
To Spot 3
在这里,SpotNo.
是随机的,由一些迭代生成。由于迭代,我只能打印如下结果:
From Spot 1
Spot 2
Spot 5
Spot 8
Spot 6
Spot 3
有什么方法可以删除我的最后一个新行Spot 3
吗?
编辑:
我想找到两个顶点之间的最短路径(使用 Floyd-Warshall 算法)。这是我的代码,它描述了以下图形:
#include <iostream>
using namespace std;
const int INF = 100000;
int n = 10, path[11][11], dist[11][11], map[11][11];
void init() {
int i, j;
for ( i = 1; i <= n; i++ )
for ( j = 1; j <= n; j++ )
map[i][j] = ( i == j ) ? 0:INF;
map[1][2] = 2, map[1][4] = 20, map[2][5] = 1;
map[3][2] = 3, map[4][3] = 8, map[4][6] = 6;
map[4][7] = 4, map[5][3] = 7, map[5][8] = 3;
map[6][3] = 1, map[7][8] = 1, map[8][6] = 2;
map[8][10] = 2, map[9][7] = 2, map[10][9] = 1;
}
void floyd() {
int i, j, k;
for ( i = 1; i <= n; i++ )
for ( j = 1; j <= n; j++ )
dist[i][j] = map[i][j], path[i][j] = 0;
for ( k = 1; k <= n; k++ )
for ( i = 1; i <= n; i++ )
for ( j = 1; j <= n; j++ )
if ( dist[i][k] + dist[k][j] < dist[i][j] )
dist[i][j] = dist[i][k] + dist[k][j], path[i][j] = k;
}
void output( int i, int j ) {
if ( i == j ) return;
if ( path[i][j] == 0 ) cout << "Spot" << j << endl;
else {
output( i, path[i][j] ); // iterations
output( path[i][j], j );
}
}
int main() {
int u, v;
init();
floyd();
u = 1, v = 3;
if ( dist[u][v] == INF ) cout << "No path" << endl;
else {
cout << "From Spot" << u << endl;
output( u, v );
cout << endl;
}
return 0;
}
现在的问题是找到最后一次迭代的条件,以便我可以cout
使用不同的表达式。但我认为通过简单地删除最后一个表达式并重写来解决问题更容易,所以我没有附上我的代码。
编辑2:
在 Fabian Tamp 的帮助下,我已经实现了我的目的,尽管我编写上面的代码似乎有点愚蠢。修改后的代码如下:
#include <iostream>
#include <queue>
using namespace std;
const int INF = 100000;
int n = 10, path[11][11], dist[11][11], map[11][11];
void init() {
int i, j;
for ( i = 1; i <= n; i++ )
for ( j = 1; j <= n; j++ )
map[i][j] = ( i == j ) ? 0:INF;
map[1][2] = 2, map[1][4] = 20, map[2][5] = 1;
map[3][3] = 3, map[4][3] = 8, map[4][6] = 6;
map[4][7] = 4, map[5][3] = 7, map[5][8] = 3;
map[6][3] = 1, map[7][8] = 1, map[8][6] = 2;
map[8][10] = 2, map[9][7] = 2, map[10][9] = 1;
}
void floyd() {
int i, j, k;
for ( i = 1; i <= n; i++ )
for ( j = 1; j <= n; j++ )
dist[i][j] = map[i][j], path[i][j] = 0;
for ( k = 1; k <= n; k++ )
for ( i = 1; i <= n; i++ )
for ( j = 1; j <= n; j++ )
if ( dist[i][k] + dist[k][j] < dist[i][j] )
dist[i][j] = dist[i][k] + dist[k][j], path[i][j] = k;
}
void output( int i, int j, queue<int> &output_queue ) {
if ( i == j ) return;
if ( path[i][j] == 0 ) output_queue.push(j);
else {
output( i, path[i][j], output_queue); // iterations
output( path[i][j], j, output_queue);
}
}
void print_path(queue<int> output_queue) {
if (output_queue.empty()) return;
int item = output_queue.front();
while (!output_queue.empty()) {
item = output_queue.front();
output_queue.pop();
if (output_queue.empty()) {
cout << "To ";
}
cout << "Spot " << item << endl;
}
}
int main() {
int u, v;
init();
floyd();
u = 1, v = 3;
if ( dist[u][v] == INF ) cout << "No path" << endl;
else {
cout << "From Spot " << u << endl;
queue<int> output_queue;
output(u, v, output_queue);
print_path(output_queue);
}
return 0;
}
输出是一开始的指南。谢谢你们!