今天看到蓝桥杯的竞赛题:兰顿蚂蚁,感觉不怎么难,刚好想写一写代码,由于是小白,代码的不规范之处还请指出来。原创:苏州大学,右京先生。曹柒夏。https://blog.csdn.net/jwsl999/article/details/85107483
前情提要:
#include <iostream>
using namespace std;
//定义上下左右方向,1代表黑格,0代表白格。
typedef enum _direction{Dup,Dright,Ddown,Dleft}Direction;
void SolveNextDirection(Direction direction, int **arr, int nRow, int nCol,int move);
int main()
{
int m, n;
int nRow, nCol, move;
char Dre;
Direction direction;
cin >> m >> n;
int **arr = new int *[m];
for (int i = 0; i < m; ++i)
{
arr[i] = new int[n];
}
for (int i = 0; i < m;++i)
for(int j = 0; j < n; ++j)
{
cin >> arr[i][j];
}
cin >> nRow >> nCol >> Dre >> move;
if (Dre == 'U')
{
direction = Dup;
}
else if (Dre == 'R')
{
direction = Dright;
}
else if (Dre == 'D')
{
direction = Ddown;
}
else
direction = Dleft;
SolveNextDirection(direction,arr,nRow,nCol,move);
delete[] arr;
return 0;
}
void SolveNextDirection(Direction direction,int **arr,int nRow,int nCol,int move)
{
int n = 0;
while (n < move)
{
if (arr[nRow][nCol] == 1)
{
if (direction == Dleft)
{
direction = Dup;
}
else
direction = (Direction)(direction + 1);
arr[nRow][nCol] = 0;
}
else
{
if (direction == Dup)
{
direction = Dleft;
}
else
direction = (Direction)(direction - 1);
arr[nRow][nCol] = 1;
}
if (direction == Dup)
nRow -= 1;
else if (direction == Dright)
nCol += 1;
else if (direction == Ddown)
nRow += 1;
else
nCol -= 1;
++n;
}
cout << nRow << " " << nCol << endl;
}
日志原创,转载请标明出处!
https://blog.csdn.net/jwsl999/article/details/85107483