SRM 447
来源:互联网 发布:如何通过源码 编辑:程序博客网 时间:2024/05/16 23:48
250:
/* * Author : Praesidio * Create Time : 2014-05-11 14:38:38 * File Name : srm447250.cpp */#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <set>#include <vector>#include <map>#include <stack>#include <ctime>#include <sstream>using namespace std;#define inf 0x3f3f3f3f#define eps 1e-10#define sqr(x) ((x)*(x))#define clr(x) memset(x,0,sizeof(x))typedef long long ll;template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();}int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}const int dx[8] = { -2 , -2 , -1 , -1 , 1 , 1 , 2 , 2 };const int dy[8] = { -1 , 1 , -2 , 2 , -2 , 2 , -1 , 1 };int n,ans;int g[12][12],f[12][12];int startx,starty;class KnightsTour {public: bool check(int,int); void init(vector<string>); void dfs(int,int);int visitedPositions(vector <string>);};bool KnightsTour::check(int x,int y) { return ( x>=0 && x < 8 && y >=0 && y < 8);}void KnightsTour::init(vector <string> board) { for (int i = 0 ; i < n ; i++ ) for (int j = 0 ; j < n ; j++) if (board[i][j]=='.') g[i][j] = 0; else if (board[i][j]=='*') g[i][j] = 1; else if (board[i][j]=='K') { startx = i; starty = j; }}void KnightsTour::dfs(int x,int y) { if (x==-1) return; ans++; for (int i = 0 ; i < n ; i++ ) for (int j = 0 ; j < n ; j++) { if (!g[i][j]) { int tmp = 0; for (int k = 0 ; k < 8 ; k++ ) { int tx = i + dx[k]; int ty = j + dy[k]; if ( !g[tx][ty] && check(tx,ty) ) tmp++; } f[i][j] = tmp; } } g[x][y] = 1; int m = 100 , nx = -1 , ny = -1; for (int i = 0 ; i < 8 ; i++) { int tx = x + dx[i]; int ty = y + dy[i]; if ( check(tx,ty) && g[tx][ty]==0 && m > f[tx][ty] ) { m = f[tx][ty]; nx = tx; ny =ty; } } dfs(nx,ny);}int KnightsTour::visitedPositions(vector <string> board) { n = (int)board.size(); init(board);ans = 0; dfs(startx,starty); return ans;}
500:
/* * Author: Praesidio * Created Time: 2014-05-18 2:01:58 * File Name: srm447500.cpp */#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <vector>using namespace std;#define For(i , n) for(int i = 0 ; i < (n) ; ++i)#define Sz(x) (int)((x).size())typedef long long lint ;const int maxint = -1u>>2 ;const double eps = 1e-6 ;struct Graph{ int g[50][50]; int n,m; int match[50]; bool chk[50]; Graph(int _n ,int _m){ n = _n; m = _m; memset(g,0,sizeof(g)); memset(match,-1,sizeof(match)); memset(chk,0,sizeof(chk)); } bool dfs( int p ) { for (int i = 0 ; i < m ; i++) { if (g[p][i] && !chk[i]) { chk[i] = true; if (match[i]==-1 || dfs(match[i] )) { match[i] = p; return true; } } } return false; } int KM() { int ans = 0; for (int i = 0 ; i < n ; i++) { memset(chk,0,sizeof(chk)); if (dfs(i)) ans++; } return ans; }};class PeopleYouMayKnow{public:int maximalScore(vector <string> friends, int person1, int person2){ int ans = 0; bool vis[50]; memset(vis,0,sizeof(vis)); for (int i = 0 ; i < (int)friends.size() ; i++) { if ( i != person1 && i !=person2 ) if (friends[i][person1]=='Y' && friends[i][person2]=='Y') { vis[i] = true; ans++; } } vector <int> left,right; for (int i = 0 ; i < Sz(friends) ; i++ ) if (friends[i][person1] =='Y' && !vis[i]) left.push_back(i); for (int i = 0 ; i < Sz(friends) ; i++ ) if (friends[i][person2] == 'Y' && !vis[i]) right.push_back(i); Graph g( Sz(left) , Sz(right) ); for (int i = 0 ; i < Sz(left) ; i++ ) for (int j = 0 ; j < Sz(right) ; j++) if (friends[left[i]][right[j]] == 'Y' ) g.g[i][j] = 1; ans += g.KM(); return int(ans) ;}};
0 0
- SRM 447
- srm
- topcode srm SRM 557
- SRM 443
- SRM 442
- SRM 439
- SRM 438
- SRM 444
- SRM 434
- SRM 445
- SRM 426
- SRM 456
- SRM 467
- SRM 466
- SRM 465
- SRM 466
- SRM 469
- SRM 470
- Android 的下拉列表框使用
- C#流程控制语句
- Opencv学习笔记之:(1)Opencv简介
- 邮件发送测试页面。前台页面
- 沿对角线填充矩阵
- SRM 447
- tiny6410裸机实验第5章--------------DDR初始化等(代码)
- 全方位的软件测试管理 - 概要描述
- 很无语,吐个槽
- 沿对角线填充矩阵
- Spring基于 Annotation 的简单介绍
- 《Linux内核设计与实现》读书笔记(十三)- 虚拟文件系统
- 最小生成树-Prim算法
- Java中的Condition --使线程间通信更高效