Walking - Questions - Daimayuan Online Judge
Ideas:
The first thought was DFS, but 2^100 must be TLE;
Then I felt that it couldn't work, I had to use DP, and DP always had to consider the previous state "Have you walked?" && "Is it enough for me to leave?"
AC code:
#include <iostream>
#include <string.h>
using namespace std;
const int N = 1e5+10;
int A[N],B[N];
int ans[101][N];
int main() {
int n,m;
memset(ans, 0, sizeof(ans));
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>A[i]>>B[i];
ans[1][A[1]]=1;
ans[1][B[1]]=1;
ans[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if( j>=A[i] && ans[i-1][j-A[i]] ) ans[i][j]=1;
if( j>=B[i] && ans[i-1][j-B[i]] ) ans[i][j]=1;
}
for(int i=0;i<=m;i++)
{
if(ans[n][i]==1) cout<<"1";
else cout<<"0";
}
return 0;
}