leetcode 980

#include <iostream>
#include<stdlib.h>
#include<string>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<functional>
#include<queue>
#include<sstream>
typedef  long long  ll;
using namespace std;
int Able_Node_Num=9;//必须经过的0的个数
int counts = 0;//记录路径的个数
bool visited[3][4] = { false };
int N = 3;
int M = 4;
int grid[3][4] = {{1,0,0,0},{0,0,0,0},{0,0,2,-1}};
//四种变化,上下左右
int changes[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };

void process(int x,int y,int num) {
	for (int i = 0; i < 4; i++) {
		int tmpx = x + changes[i][0];
		int tmpy = y + changes[i][1];
		//在边界范围内,且不为-1或者1;
		if (tmpx >= 0 && tmpx<N&&tmpy >= 0 && tmpy<M && grid[tmpx][tmpy]!=-1&& grid[tmpx][tmpy] != 1) {
			if (grid[tmpx][tmpy] == 2 ) {
				if (num == Able_Node_Num) {
					printf("------------grid[%d][%d]=%d  num=%d\n ", x, y, grid[x][y], num);
					counts++;
					return;
				}
				//return;
			}
			else {   //grid[tmpx][tmpy]==0			
				if (visited[tmpx][tmpy] == false) {
					printf("grid[%d][%d]=%d  num=%d\n ", tmpx, tmpy, grid[tmpx][tmpy],num+1);
					visited[tmpx][tmpy] = true;
					process(tmpx, tmpy, num+1);  //注意和num++的区别
					//还原
					visited[tmpx][tmpy] = false;
				}
			}
		}

	}
}
int main() {
	process(0, 0, 0);
	cout << counts;
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值