为什么运行时move函数中第二层if条件为真但不进入,求教
查看原帖
为什么运行时move函数中第二层if条件为真但不进入,求教
1481542
Febrie_楼主2024/10/16 23:21
#include<iostream>
#include<string.h>
using namespace std;
char map[12][12];
struct man{
	int flag;
	int x;
	int y;
};man cow,farm;

void move(man *p){
	if (p->flag == 0) {
		if ((map[p->x - 1][p->y]) == '*') {
		p->flag == 1;
		//cout << farm.flag << endl;
	}
	else p->x--;
	}
	else if (p->flag == 1) {
	if (map[p->x][p->y + 1] == '*') {
		p->flag == 2;
	}
	else p->y++;
	}
	else if (p->flag == 2) {
	if (map[p->x + 1][p->y] == '*') {
		p->flag == 3;
	}
	else p->x++;
	}
	else if (p->flag == 3) {
	if (map[p->x][p->y - 1] == '*') {
		p->flag == 0;
	}
	else p->y--;
	}
	//cout << p->x << p->y << endl;
}

int main() {
	memset(map,0,sizeof(map));
	for(int x=1;x<11;x++) {
		for(int y=1;y<11;y++) {
			cin>>map[x][y];
			if(map[x][y]=='F') {
				farm.x=x;
				farm.y=y;
			}
			else if(map[x][y]=='C') {
				cow.x=x;
				cow.y=y;
			}
		}
	}
	for(int i=0;i<12;i+=11) {
		for(int j=0;j<12;j++) map[i][j]='*';
	}
	for(int i=0;i<12;i+=11) {
		for(int j=0;j<12;j++) map[j][i]='*';
	}//边界设置为* 
	cow.flag=0;
	farm.flag=0;
	int cnt=0;
	man*pp=&farm;
	man*qq=&cow;
	while(cow.x!=farm.x||cow.y!=farm.y) {
		cnt++;
		move(pp);
		move(qq);
		if(cnt>500000) {
			cnt=0;
			break;
		}
	}
	cout<<cnt;
	return 0;
}
2024/10/16 23:21
加载中...