Topcoder SRM 148 Div2 1000(dfs搜索+hash判重)
来源:互联网 发布:大数据技术入门 pdf 编辑:程序博客网 时间:2024/05/22 07:01
Problem Statement
9 numbers need to be arranged in a magic number square. A magic number square is a square of numbers that is arranged such that every row and column has the same sum. For example:
1 2 33 2 12 2 2
Create a class MNS containing a method combos which takes as an argument a vector <int>numbers and returns the number of distinct ways those numbers can be arranged in a magic number square. Two magic number squares are distinct if they differ in value at one or more positions. For example, there is only one magic number square that can be made of 9 instances of the same number.
Definition
Class:MNSMethod:combosParameters:vector <int>Returns:intMethod signature:int combos(vector <int> numbers)(be sure your method is public)Limits
Time limit (s):2.000Memory limit (MB):64Notes
-Unlike some versions of the magic number square, the numbers do not have to be unique.Constraints
-numbers will contain exactly 9 elements.-each element of numbers will be between 0 and 9, inclusive.Examples
0){1,2,3,3,2,1,2,2,2}
Returns: 181)
{4,4,4,4,4,4,4,4,4}
Returns: 12)
{1,5,1,2,5,6,2,3,2}
Returns: 363)
{1,2,6,6,6,4,2,6,4}
Returns: 0
dfs搜索。用string存储图形,map<string,bool>判重。
能省就省。
这种题应该从想到写15分钟内搞定。太慢啦。
//Hello. I'm Peter.#include<cstdio>#include<iostream>#include<sstream>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>#include<functional>#include<cctype>#include<ctime>#include<stack>#include<queue>#include<vector>#include<set>#include<map>using namespace std;typedef long long ll;typedef long double ld;#define peter cout<<"i am peter"<<endl#define input freopen("data.txt","r",stdin)#define randin srand((unsigned int)time(NULL))#define INT (0x3f3f3f3f)*2#define LL (0x3f3f3f3f3f3f3f3f)*2#define gsize(a) (int)a.size()#define len(a) (int)strlen(a)#define slen(s) (int)s.length()#define pb(a) push_back(a)#define clr(a) memset(a,0,sizeof(a))#define clr_minus1(a) memset(a,-1,sizeof(a))#define clr_INT(a) memset(a,INT,sizeof(a))#define clr_true(a) memset(a,true,sizeof(a))#define clr_false(a) memset(a,false,sizeof(a))#define clr_queue(q) while(!q.empty()) q.pop()#define clr_stack(s) while(!s.empty()) s.pop()#define rep(i, a, b) for (int i = a; i < b; i++)#define dep(i, a, b) for (int i = a; i > b; i--)#define repin(i, a, b) for (int i = a; i <= b; i++)#define depin(i, a, b) for (int i = a; i >= b; i--)#define pi 3.1415926535898#define eps 1e-6#define MOD 1000000007#define MAXN 100100#define N#define Mint ans;map<string,bool>app;void dfs(vector<int>ma[3],vector<int>belong,vector<int>d){ int len=gsize(belong); if(len==0) { int t1,t2,t3,t4,t5,t6,t7,t8,t9,sum1; t1=ma[0][0],t2=ma[0][1],t3=ma[0][2]; t4=ma[1][0],t5=ma[1][1],t6=ma[1][2]; t7=ma[2][0],t8=ma[2][1],t9=ma[2][2]; string s; s.clear(); s+=d[t1]+'0';s+=d[t2]+'0';s+=d[t3]+'0'; s+=d[t4]+'0';s+=d[t5]+'0';s+=d[t6]+'0'; s+=d[t7]+'0';s+=d[t8]+'0';s+=d[t9]+'0'; if(!app[s]) app[s]=true; else return; sum1=d[t1]+d[t2]+d[t3]; int sum2; sum2=d[t4]+d[t5]+d[t6]; if(sum1!=sum2) return; sum2=d[t7]+d[t8]+d[t9]; if(sum1!=sum2) return; sum2=d[t1]+d[t4]+d[t7]; if(sum1!=sum2) return; sum2=d[t2]+d[t5]+d[t8]; if(sum1!=sum2) return; sum2=d[t3]+d[t6]+d[t9]; if(sum1!=sum2) return; ans++; return; } int r=0; if(len==9) r=0; else if(len==6) r=1; else if(len==3) r=2; rep(i,0,len) { rep(j,0,len) { if(j==i ) continue; rep(k,0,len) { if(k==i || k==j) continue; vector<int>nextbelong; nextbelong.clear(); if(len>3) { rep(h,0,len) { if(h==i || h==j || h==k) continue; nextbelong.pb(belong[h]); } } ma[r].clear(); ma[r].pb(belong[i]); ma[r].pb(belong[j]); ma[r].pb(belong[k]); dfs(ma,nextbelong,d); } } }}class MNS{public: int combos(vector <int> numbers) { ans=0; vector<int>ma[3],belong; rep(i,0,3) { ma[i].clear(); } belong.clear(); rep(i,0,9) { belong.pb(i); } app.clear(); dfs(ma,belong,numbers); return ans; }};
0 0
- Topcoder SRM 148 Div2 1000(dfs搜索+hash判重)
- Topcoder SRM 146 Div2 1000(dfs搜索,经典过桥问题,很有意思)
- Topcoder SRM 635 div2 1000
- Topcoder SRM 636 div2 1000
- TopCoder SRM 634 Div2 Problem 1000 - SpecialStrings
- Topcoder SRM 640 Div2 1000(巧妙数学题)
- Topcoder SRM 517 DIV2 1000 CuttingGrass
- Topcoder SRM 566 DIV2 1000 FencingPenguinsEasy
- Topcoder SRM 549 DIV2 1000 OrderOfTheHats
- Topcoder SRM 616 Div2 1000 TwoLLogo
- Topcoder SRM 561 DIV2 1000 FoxAndTouristFamilies
- topcoder SRM 513 DIV2
- TopCoder SRM 543 DIV2
- topcoder-srm-233-div2
- Topcoder SRM Div2 Level2
- TopCoder SRM 144 div2
- TOPCODER SRM 612 DIV2
- TOPCODER SRM 613 DIV2
- 程序员学习笔记---java线程的学习
- 哈哈小牛的第一次写博客
- CTCC第6站北京站:超级组王睿被罚江腾一冠军
- HDU 5005(Compromise-双人目标为最大化不同值的博弈)
- expect点滴
- Topcoder SRM 148 Div2 1000(dfs搜索+hash判重)
- hive的配置与使用
- python学习笔记(三)
- cocos2dx-2.x 打包APK(学习笔记 Python + JDK + ADT + NDK )
- ignore 用法
- 黑马程序员——Java集合框架(2)
- 位运算--一个整数的二进制表示中1的个数
- 删除一个服务
- 搭建一个免费的,无限流量的Blog----github Pages和Jekyll入门