前两天笔试腾讯数据分以岗,有一个题比较有意思
#include
#include
#include
#include
using namespace std;
void dfs(vector<vector<int>> flag,int &sum,int x,int y)
{
if(x>3||y>3)
return ;
if(x==3&&y==3)
{
sum++;
return;
}
if(!flag[x][y])
{
flag[x][y]=1;
dfs(flag,sum,x+1,y);
dfs(flag,sum,x+2,y);
dfs(flag,sum,x,y+2);
dfs(flag,sum,x+3,y);
dfs(flag,sum,x,y+3);
dfs(flag,sum,x,y+1);
flag[x][y]=0;
}
}
int main()
{
vector<vector<int>> flag(4,vector<int>(4,0));
int sum=0;
dfs(flag,sum,0,0);
cout<return 0;
}
运行结果106
实质是一个运筹学的动态规划问题,考虑步长1-3
考完见网上有大佬各种解法,也有用图解dfs深度优先解的
该解法来自 牛客网用户:1eE
#include
using namespace std;
#define N 10 int DFS[N][N]={0};
int dfs(int m, int n){
for(int i=0; i < m; ++i){ if(DFS[i][n]){ DFS[m][n] += DFS[i][n]; }else{ DFS[m][n] += dfs(i, n); } }
for(int j = 0; j < n; ++j){ if(DFS[m][j]){ DFS[m][n] += DFS[m][j]; }else{ DFS[m][n] += dfs(m, j); }
} DFS[n][m] = DFS[m][n]; return DFS[m][n];
}
int main(){ DFS[0][0] = 1;
cout << dfs(3,3) <return 0;
}
甚至有大佬这样
简单粗暴,不明觉厉hahhah
给跪了
这里顺便提一句第二个大佬DFS解法里的头文件引起了我的注意,于是了解了一下(本文标题中# <>等字符无法使用,所以去掉了)
#include
当我发现这个头文件的时候
说不出话来(捂脸)
这个是C++版本升级,然后文件自带的,#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
等等……
codeforces说明:
- solution to include bits/stdc++.h in visual c++
By Tensor, 2 years ago, ,
This is just a method i used to make me able to include bits/stdc++.h
in visual c++.
for those had minGW installed on PC :
C:\MinGW\mingw32\lib\gcc\mingw32\4.8.1\include\c++\mingw32\bits
copy this folder and then go to this adress
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
paste your folder. go to your visual studio project type bits you will see
the auto-complete for the library and then choose stdc++.h
for those don’t have minGW:
you should write your own header file and include all libraries in it then
go to C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
make new folder name it “bits” and name the header file stdc++.h
then paste it in “bits” folder.
Hope this helps!
Happy coding
就是说
1.如果安装了MinGW的直接在文件夹里面找到bits这个文件夹(注意里面一定要包含stdc++.h,貌似我的版本有两个bits文件夹),把里面内容复制粘贴到vs的头文件库(一般是在c盘的安装目录)里面。
(2017.3.19修正,装VS2017时候找了好一会,已经不是在这个地方,应该是先找到VS主目录,然后往这里面找VS主要目录\VC\Tools\MSVC\14.10.25017\include)
2.如果没有安装MinGW,那么自己在vs的include文件夹里面新建一个bits文件夹,里面新建一个名叫stdc++.h的头文件,里面写你常用的头文件,这里搬运一下stdc++.h源文件:
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// .
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if __cplusplus >= 201103L
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
// C++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if __cplusplus >= 201103L
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
不过在国内oj中,poj,hdu 不支持这个函数,这几个oj的编译器问题,其他国外的oj,还有台湾的oj都支持,CF,Topcoder也都支持。
参考:https://blog.csdn.net/dragon60066/article/details/56529077
https://www.cnblogs.com/Kiven5197/p/5745589.html
https://blog.kuoe0.tw/posts/2014/01/31/install-gnu-gcc-on-os-x-and-use-the-header-bits-stdcplusplus-h-and-policy-based-data-structure/