UVA-11795 (pressure DP)

Source: Internet
Author: User

Test instructions

Destroy n Robots in a certain order, each robot can be destroyed by the weapon, each robot only with a specific weapon to destroy, now given an initial weapon, it can destroy some robots, each robot's weapon can destroy those robots also give you, now you want to destroy N robot order Total ;


DP[I][K] Indicates the number of operations of a robot that has been hung by using K at the time of the first operation;

The robot j;dp[i][x]+=dp[i-1][k];x that enumerates the secondary attacks is the state that hangs to J after K and J;

AC Code:

/************************************************┆┏┓┏┓┆┆┏┛┻━━━┛┻┓┆┆┃┃┆┆┃━┃┆┆┃┳┛┗┳┃┆┆┃  ┃┆┆┃┻┃┆┆┗━┓┏━┛┆┆┃┃┆┆┃┗━━━┓┆┆┃ac Horse ┣┓┆┆┃┏┛┆┆┗┓┓┏━┳┓┏┛┆ ┆┃┫┫┃┫┫┆┆┗┻┛┗┻┛┆************************************************ * * #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h > #include <stack>using namespace std, #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (ss,b    , sizeof (SS)); typedef long LONG ll;template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 ';    f= ch== '-', Ch=getchar ()); for (num=0;    ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);}    int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = P%10, p/=10;    while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0), const int Inf=1e9;const int n= (1<<18), const int maxn= (1&LT;&LT;8); Const double Eps=1e-8;char s[20][20];int num[20],sword[n],n;    LL dp[18][n];inline int Getnum (int x) {//int len=strlen (s[x]);    int ans=0;    for (i,0,n-1) {if (s[x][i]== ' 1 ') ans+= (1<<i); } return ans;        int main () {int t,case=0;        Read (t);            while (t--) {read (n);            scanf ("%s", S[0]);            Sword[0]=getnum (0);            MST (dp,0);                for (i,1,n) {scanf ("%s", S[i]);            Num[i]=getnum (i);                } for (i,1, (1<<n)-1)//preprocessing the resulting weapon condition in each state {sword[i]=sword[0];                for (int j=0;j<n;j++) {if (i& (1<<j)) sword[i]= (sword[i]|num[j+1]);            }} dp[0][0]=1;for (I,1,n) {for (j,1,n) {for (int k=0;k< (1<<n); k++)                        {if (k& (1<< (j-1))) continue;//If the first J robot has been hung off; if (sword[k]& (1<< (j-1))) dp[i][k| ( 1<< (j-1))]+=dp[i-1][k];//If there is a weapon to kill the first J robot}} LL ans=dp[n][(            1<<n)-1];        printf ("Case%d:%lld\n", ++case,ans); } return 0;}



UVA-11795 (pressure DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.