用C++语言改变这个代码的排版,所呈现的效果不变 #include <stdio.h> #include <vector> #include <queue> #include <string> using namespace std; vector<vector<char> >board; int m; int n; struct Position { int x; int y; Position(int x1,int y1): x(x1),y(y1){} }; void dispboard() {for(int i=0;i<m;i++) {printf(" "); for(int j=0;j<n;j++) printf("%c",board[i][j]); printf("\n"); } } void BFS(int i,int j,int m,int n) {queue<Position *>qu; Position * pos=new Position(i,j); qu.push(pos); board[i][j]='*'; while(!qu.empty()) {Position * curp=qu.front(); qu.pop(); if (curp->x>0 && board[curp->x-1][curp->y]=='O') {Position * up=new Position(curp->x-1,curp->y); qu.push(up); board[up->x][up->y]='*'; } if(curp->x<m-1 && board[curp->x+1][curp->y]=='O') {Position * down=new Position(curp->x+1,curp->y); qu.push(down); board[down->x][down->y]='*'; } if(curp->y>0 && board[curp->x][curp->y-1]=='O') {Position * left=new Position(curp->x,curp->y-1); qu.push(left); board[left->x][left->y]='*'; } if(curp->y<n-1 && board[curp->x][curp->y+1]=='O') {Position * right=new Position(curp->x,curp->y+1); qu.push(right); board[right->x][right->y]='*'; } delete curp; } } void solve() { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) if(board[i][j]=='O') { if(i==0 || i==m-1 || j==0 || j==n-1) BFS(i,j,m,n); } printf("BFS后的面板:\n");dispboard(); for(i=0;i<m;i++) for(j=0;j<n;j++) { if(board[i][j]=='O') board[i][j]='X'; else if(board[i][j]=='*') board[i][j]='O'; } } void main() {string str[]={"XXXX","XOOX","XXOX","XOXX"}; m=4;n=4; for (int i=0;i<m;i++) {vector<char>s; for (int j=0;j<n;j++) s.push_back(str[i][j]); board.push_back(s); } printf("原始面板:\n"); dispboard(); solve(); printf("最后面板:\n"); dispboard(); }
时间: 2024-04-21 19:26:55 浏览: 95
#include <stdio.h>
#include <vector>
#include <queue>
#include <string>
using namespace std;
vector<vector<char>> board;
int m;
int n;
struct Position {
int x;
int y;
Position(int x1, int y1) : x(x1), y(y1) {}
};
void dispboard() {
for (int i = 0; i < m; i++) {
printf(" ");
for (int j = 0; j < n; j++) {
printf("%c", board[i][j]);
}
printf("\n");
}
}
void BFS(int i, int j, int m, int n) {
queue<Position*> qu;
Position* pos = new Position(i, j);
qu.push(pos);
board[i][j] = '*';
while (!qu.empty()) {
Position* curp = qu.front();
qu.pop();
if (curp->x > 0 && board[curp->x - 1][curp->y] == 'O') {
Position* up = new Position(curp->x - 1, curp->y);
qu.push(up);
board[up->x][up->y] = '*';
}
if (curp->x < m - 1 && board[curp->x + 1][curp->y] == 'O') {
Position* down = new Position(curp->x + 1, curp->y);
qu.push(down);
board[down->x][down->y] = '*';
}
if (curp->y > 0 && board[curp->x][curp->y - 1] == 'O') {
Position* left = new Position(curp->x, curp->y - 1);
qu.push(left);
board[left->x][left->y] = '*';
}
if (curp->y < n - 1 && board[curp->x][curp->y + 1] == 'O') {
Position* right = new Position(curp->x, curp->y + 1);
qu.push(right);
board[right->x][right->y] = '*';
}
delete curp;
}
}
void solve() {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (board[i][j] == 'O') {
if (i == 0 || i == m - 1 || j == 0 || j == n - 1)
BFS(i, j, m, n);
}
}
}
printf("BFS后的面板:\n");
dispboard();
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (board[i][j] == 'O')
board[i][j] = 'X';
else if (board[i][j] == '*')
board[i][j] = 'O';
}
}
}
int main() {
string str[] = { "XXXX","XOOX","XXOX","XOXX" };
m = 4;
n = 4;
for (int i = 0; i < m; i++) {
vector<char> s;
for (int j = 0; j < n; j++) {
s.push_back(str[i][j]);
}
board.push_back(s);
}
printf("原始面板:\n");
dispboard();
solve();
printf("最后面板:\n");
dispboard();
return 0;
}
阅读全文