放个题目,如下:第一次自己做的黄题,解法如下:
#include<iostream>
using namespace std;
int D[2] = { 1,-1 };
int c1, c2, f1, f2, n = 0, cd = 3, fd = 3;
char map[12][12] = {};
void next(int* x, int* y, int* d);
int main()
{
for (int i = 0; i <= 11; i++) map[i][0] = '*', map[i][11] = '*'; /*建立墙壁*/
for (int i = 1; i <= 11; i++) map[0][i] = '*', map[11][i] = '*';
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
{
cin >> map[i][j];
if (map[i][j] == 'C') { c1 = i; c2 = j; }
if (map[i][j] == 'F') { f1 = i; f2 = j; }
}
for (; c1 != f1 || c2 != f2;)
{
next(&c1, &c2, &cd);
next(&f1, &f2, &fd);
n++;
if (n == 99999) {
cout << '0' << endl; return 0; /*循环了,直接跳出*/
}
}
cout << n << endl;
}
void next(int* x, int* y, int* d)
{
if ((*d) % 2 != 0 && map[(*x) + D[(*d) / 2]][(*y)] != '*') /*判断上下移动还是左右移动*/
(*x) += D[(*d) / 2]; /*并且判断移动方向是否阻挡*/
else if ((*d) % 2 == 0 && map[(*x)][(*y) + D[(*d) / 2]] != '*')
(*y) += D[(*d) / 2];
else
{
(*d)++;
if ((*d) == 4) (*d) = 0; /*改变方向*/
}
}
像屎山了(