题目
思路很简单, 唯一的难点是如何判断找不到,最开始我的思路是搜索到一定次数还没有找到就输出0,在题解看到了一种更好的方法,通过特征值去判断有没有陷入循环,如图
他是开了一个160000的数组去保存状态,我是通过map保存。
#include <iostream>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
struct Coordinate{
int row;
int col;
};
Coordinate mov_dir[4] = {
{
-1, 0}, {
0, 1}, {
1, 0}, {
0, -1}};
map<int, int> mp;
int main()
{
ios::sync_with_stdio