题意:问图是否满足八皇后。
解题思路:hash,dp,位运算
解题代码:
我的搓代码。
1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月14日 星期六 12时00分44秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 26 using namespace std; 27 char str[10][10]; 28 int hsh[10]; 29 int hsl[10]; 30 int hshl[50]; 31 int hslh[50]; 32 int main(){ 33 while(scanf("%s",&str[1][1]) != EOF) 34 { 35 for(int i = 2;i <= 8 ;i ++) 36 { 37 scanf("%s",&str[i][1]); 38 } 39 memset(hsh,0,sizeof(hsh)); 40 memset(hsl,0,sizeof(hsh)); 41 memset(hshl,0,sizeof(hshl)); 42 memset(hslh,0,sizeof(hslh)); 43 int ok = 0 ; 44 for(int i= 1;i <= 8;i ++) 45 { 46 for(int j = 1;j <= 8 ;j ++) 47 { 48 if(str[i][j] == ‘.‘) 49 continue; 50 if(hsh[i] == 1) 51 { 52 ok = 1; 53 }else{ 54 hsh[i] = 1 ; 55 } 56 57 if(hsl[j] == 1) 58 { 59 ok = 1; 60 }else{ 61 hsl[j] = 1 ; 62 } 63 64 if(hshl[j-i+25] == 1) 65 { 66 ok = 1; 67 }else{ 68 hshl[j-i +25] = 1 ; 69 } 70 71 if(hslh[j+i] == 1) 72 { 73 ok = 1; 74 }else{ 75 hslh[j+i] = 1 ; 76 } 77 } 78 79 } 80 if(ok == 1 ) 81 printf("invalid\n"); 82 else printf("valid\n"); 83 } 84 85 86 return 0; 87 }
然后看到了学弟的位运算代码:
1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 char chess[10][10]; 7 int stu1 = 0, stu2 = 0, flag = 0; 8 int main() { 9 for (int i = 0; i < 8; i++) gets (chess[i]); 10 for (int i = 0; i < 8; i++) { 11 for (int j = 0; j < 8; j++) 12 if (chess[i][j] == ‘*‘) 13 if (! (stu1 & 1 << j) && ! (stu2 & 1 << j)) 14 stu1 |= 1 << j, stu2 |= 1 << j; 15 else { 16 flag = 1;break; 17 } 18 if (flag) break; 19 stu1 <<= 1, stu2 >>= 1; 20 } 21 if(flag) puts("invalid"); 22 else puts("valid"); 23 }
ACM-ICPC North America Qualifier 2014 Eight Queens
原文:http://www.cnblogs.com/zyue/p/4338344.html