简洁的做法可是错了
查看原帖
简洁的做法可是错了
742157
ZYK_luogu楼主2023/7/1 10:29

ff 是表示朝向, 0是向上,1是向右,2是向下,3是向左。

#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 15
#define INF 1000000

int step = 0;
char ch[maxn][maxn];
int x1, y1, f1 = 0;
int x2, y2, f2 = 0;
int wk[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

void move(int &x, int &y, int &f) {
	int dx = x + wk[f][0], dy = y + wk[f][1];
	if(ch[dx][dy] == '*')
		f ++;
	else
		x = dx, y = dy;
	if(f > 3)
		f = 0;
}

int main() {
	for(int i = 0; i <= 11; i ++)
		ch[i][0] = '*', ch[i][11] = '*';
	for(int i = 0; i <= 11; i ++)
		ch[0][i] = '*', ch[11][i] = '*';
	for(int i = 1; i <= 10; i ++)
		for(int j = 1; j <= 10; j ++) {
			cin >> ch[i][j];
			if(ch[i][j] == 'C')
				x1 = i, y1 = j;
			else if(ch[i][j] == 'F')
				x2 = i, y2 = j;
		}
	while(step < INF) {
		printf("x1=%d, y1=%d, f1=%d; x2=%d, y2=%d, f2=%d; step=%d\n", x1, y1, f1, x2, y2, f2, step);
		move(x1, y1, f1);
		move(x2, y2, f2);
		step ++;
		if(x1 == x2 && y1 == y2)
			break;
	}
	cout << (step == INF) ? 0 : step;
	return 0;
}

2023/7/1 10:29
加载中...