The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C++ Code
Posted by: 2002 Inv Round 4 500 | February 14, 2011 @ 9:51am
C++ Code
[
Download
]
/* FORHAD-SUST-BD */ #include <iostream> #include <string> #include <map> #include <queue> #include <stack> #include <algorithm> #include <list> #include <set> #include <cmath> #include <cstring> #include <stdio.h> #include <string.h> #include <sstream> #include <stdlib.h> #include <vector> #include <iomanip> using namespace std; #ifdef __GNUC__ typedef long long ll;typedef unsigned long long ull; #else typedef __int64 ll; typedef unsigned __int64 ull; #endif #define INF 1<<28 #define SIZE 100 #define PI 2*acos(0) #define fg %I64d #define REP(i,n) for (i=0;i<n;i++) #define REV(i,n) for (i=n;i>=0;i--) #define FOR(i,p,k) for (i=p; i<k;i++) #define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it) #define bug(x) cout<< "->" <<#x<<": "<<x<<endl #define Sort(x) sort(x.begin(),x.end()) #define Reverse(x) reverse(x.begin(),x.end()) #define MP(a,b) make_pair(a,b) #define Clear(x,with) memset(x,with,sizeof(x)) #define Copy(c,r) memcpy(c,r,sizeof(r)) #define SZ(x) (int)x.size() #define length(x) (int)x.length() #define All(x) x.begin(),x.end() #define pb push_back #define popcount(i) __builtin_popcount(i) #define gcd(a,b) __gcd(a,b) #define fs first #define sc second #define two(X) (1<<(X)) #define twoL(X) (((int64)(1))<<(X)) #define contain(S,X) (((S)&two(X))!=0) #define containL(S,X) (((S)&twoL(X))!=0) typedef pair<int,int> pii; typedef pair<double,double> pdd; typedef vector<int> vi; typedef vector<double>vd; typedef vector<ll>vll; typedef vector<string> vs; typedef vector<vi>vvi; typedef vector<vll>vvll; typedef vector<vd>vvd; typedef vector<pii>vpii; typedef map<string,int> msi; typedef map<int,int>mii; typedef map<pii,int>mpi; template<class T> inline T sqr(T x){return x*x;} template<class T> inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;} template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No. template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();} int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;} ll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;} template<class T> void debug(const T& e){cout<<e<<endl;} template<class T1,class T2> void debug(const T1& e1,const T2& e2){cout<<e1<<"\t"<<e2<<endl;} template<class T1,class T2,class T3> void debug(const T1& e1,const T2& e2,const T3& e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;} template<class T1,class T2,class T3,class T4> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;} template<class T1,class T2,class T3,class T4,class T5> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;} template<class T> void debug(vector<T>& e){int i;REP(i,SZ(e)) cout<<e[i]<<" ";cout<<endl;} template<class T> void debug(vector< basic_string<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j];cout<<endl;} cout<<endl;} template<class T> void debug(vector< vector<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j]<<"\t";cout<<endl;} cout<<endl;} template<class T> void debug(T e[SIZE][SIZE],int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<" ";cout<<endl;}} ll Pow(ll B,ll P){ ll R=1; while(P>0) {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p //struct pq{ int cost,node;bool operator<(const pq &b)const{return cost>b.cost;}};// Min Priority Queue //bool comp(pq a,pq b){ return a.cost > b.cost;} //Comp Sort int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction //int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction //int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction vector<int>ans; int mat[SIZE][SIZE],row,col; vs grid; void BFS(int x,int y) { //debug(x,y); int i,m,n; queue<pii>Q; Q.push(MP(x,y)); mat[x][y]=0; while(!Q.empty()) { x=Q.front().first; y=Q.front().second; Q.pop(); REP(i,4) { m=x+dx[i]; n=y+dy[i]; if(m>=0 && m<row && n>=0 && n<col) { if(grid[m][n]!='#' && mat[m][n]==-1) { mat[m][n]=mat[x][y]+1; Q.push(MP(m,n)); } } } } } class Socialize { public: int average(vector <string> layout) { int i,j,sum,cnt,x,y,res; vector<pii>store; grid=layout; row=SZ(layout); col=SZ(layout[0]); REP(i,row) { REP(j,col) { if(layout[i][j]=='P') { store.pb(MP(i,j)); } } } sum=cnt=0; REP(i,SZ(store)) { Clear(mat,-1); x=store[i].first; y=store[i].second; BFS(x,y); REP(j,SZ(store)) { x=store[j].first; y=store[j].second; if(mat[x][y]>0) { cnt++; sum+=mat[x][y]; } } } //debug(sum,cnt); if(cnt==0) return 0; res=(sum/(cnt+0.0)+0.5); return res; } }; template<typename T> void print( T a ) { cerr << a; } void print( long long a ) { cerr << a << "L"; } void print( string a ) { cerr << '"' << a << '"'; } template<typename T> void print( vector<T> a ) { cerr << "{"; for ( int i = 0 ; i != a.size() ; i++ ) { if ( i != 0 ) cerr << ", "; print( a[i] ); } cerr << "}" << endl; } template<typename T> void assert_eq( int n, T have, T need ) { if ( have == need ) { cerr << "Case " << n << " passed." << endl; } else { cerr << "Case " << n << " failed: expected "; print( need ); cerr << " received "; print( have ); cerr << "." << endl; } } template<typename T> void assert_eq( int n, vector<T> have, vector<T> need ) { if( have.size() != need.size() ) { cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements."; print( have ); print( need ); return; } for( int i= 0; i < have.size(); i++ ) { if( have[i] != need[i] ) { cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "."; print( have ); print( need ); return; } } cerr << "Case " << n << " passed." << endl; } void assert_eq( int n, string have, string need ) { if ( have == need ) { cerr << "Case " << n << " passed." << endl; } else { cerr << "Case " << n << " failed: expected "; print( need ); cerr << " received "; print( have ); cerr << "." << endl; } } int main( int argc, char* argv[] ) { Socialize objectSocialize; //test case0 vector <string> param00; param00.push_back("P"); int ret0 = objectSocialize.average(param00); int need0 = 0; assert_eq(0,ret0,need0); //test case1 vector <string> param10; param10.push_back("#"); int ret1 = objectSocialize.average(param10); int need1 = 0; assert_eq(1,ret1,need1); //test case2 vector <string> param20; param20.push_back("P#P"); param20.push_back("P#."); param20.push_back("P#P"); int ret2 = objectSocialize.average(param20); int need2 = 2; assert_eq(2,ret2,need2); //test case3 vector <string> param30; param30.push_back("P...P"); param30.push_back("###.."); param30.push_back("P...#"); param30.push_back("####P"); int ret3 = objectSocialize.average(param30); int need3 = 6; assert_eq(3,ret3,need3); }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.51 / 8
57 Votes
http://codebin.yi.org/1121
page generated in 0.00 seconds