腾讯笔试题-《绝地求生》装备题/关于万能头文件"bits/stdc++.h"

前两天笔试腾讯数据分以岗,有一个题比较有意思

  • 《绝地求生》游戏中,共有1-3个等级的头盔,1-3个等级的防弹衣,结社你从无头盔,无防弹衣开始,每次只捡取没有的装备,装备换成高等级的对应装备,那么到达3级头盔,3级防弹衣,总共有多少种方法,
    <比如用(x,y)表示当前(头盔,防弹衣)的级别,0无对应装备,则(0,0)->(1,0)->(1,3)-(3,3)为一种方法>
    四个选择 6、20、64、106
 #include<iostream>
 #include<set>
 #include<queue>
 #include<vector>
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<<sum<<endl;
    return 0;
}

运行结果106

实质是一个运筹学的动态规划问题,考虑步长1-3

考完见网上有大佬各种解法,也有用图解dfs深度优先解的

该解法来自 牛客网用户:1eE

#include<bits/stdc++.h>
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) <<endl;
  return 0;
}

甚至有大佬这样

这里写图片描述

简单粗暴,不明觉厉hahhah

给跪了


这里顺便提一句第二个大佬DFS解法里的头文件引起了我的注意,于是了解了一下(本文标题中# <>等字符无法使用,所以去掉了)

 #include<bits/stdc++.h>

当我发现这个头文件的时候
说不出话来(捂脸)

这个是C++版本升级,然后文件自带的,#include

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

等等……

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
// <http://www.gnu.org/licenses/>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#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/

猜你喜欢

转载自blog.csdn.net/ego_bai/article/details/79866651