标签:
Problem Statement |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You have a rectangular board that is placed vertically. The board is divided into a grid of unit square cells. Some grid cells contain obstacles and some cells contain a grain of sand. All other cells are currently empty. You are given the description of the board as a String[] board. The elements ofboard correspond to rows of the grid in the order from top to bottom. (E.g.,board[0] represents the topmost row of cells.) Each character in each element ofboard represents one cell. The character ‘x‘ represents a cell with an obstacle, ‘o‘ is a grain of sand, and ‘.‘ (period) is an empty cell. You would like to implement a simulation of falling sand. The rules are as follows:
Return the final configuration of the board after all grains of sand reach their final locations. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Definition |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Limits |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constraints |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- | board will contain between 1 and 50 elements, inclusive. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- | Each element of board will have length between 1 and 50, inclusive. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- | All elements of board will have the same length. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- |
Each character in each element of board will be one of ‘x‘, ‘o‘, and ‘.‘
题意:模拟过程。(250分) 题解:具体就是如果上面一层有o,且当前层为.就一直做下去。
#include <iostream> #include <cstring> #include <deque> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <list> #include <map> #include <set> #include <string> #include <vector> #include <cstdio> #include <functional> #include <algorithm> typedef long long LL; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int,int>pil; class FallingSand { public: vector <string> simulate( vector <string> board ) { int n=board.size(); int m=board[0].size(); int flag=1; while(flag) { flag=0; for(int i=1;i<n;i++) { for(int j=0;j<m;j++) { if(board[i-1][j]=='o'&&board[i][j]=='.') { board[i-1][j]='.'; board[i][j]='o'; flag=1; } } } } return board; } };
题意:上下两行,每行相邻有路径,然后让你在上下两行加权值为0的边问你两点的最短路径最长为多少。(500分) 题解:n太小,直接状压跑floyd即可。
#include <iostream> #include <cstring> #include <deque> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <list> #include <map> #include <set> #include <string> #include <vector> #include <cstdio> #include <functional> #include <algorithm> typedef long long LL; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int,int>pil; int mp[30][30],m[30][30],ans,n,N; void floyd() { for(int i=0;i<N;i++) for(int j=0;j<N;j++) m[i][j]=mp[i][j]; for(int k=0;k<N;k++) for(int i=0;i<N;i++) for(int j=0;j<N;j++) m[i][j]=min(m[i][j],m[i][k]+m[k][j]); int s=0; for(int i=0;i<N;i++) for(int j=0;j<N;j++) s=max(s,m[i][j]); ans=min(ans,s); } class BridgeBuildingDiv2 { public: int minDiameter( vector <int> a, vector <int> b, int K ) { n=a.size()+1; N=n*2; CLEAR(mp,INF); ans=INF; for(int i=0;i<n-1;i++) mp[i][i+1]=mp[i+1][i]=a[i]; for(int i=0;i<n-1;i++) mp[n+i][n+1+i]=mp[n+1+i][n+i]=b[i]; for(int i=0;i<N;i++) mp[i][i]=0; int st=(1<<n)-1; for(int i=0;i<=st;i++) { int cnt=0; for(int j=0;j<n;j++) if(i&(1<<j)) cnt++; if(cnt==K) { for(int j=0;j<n;j++) if(i&(1<<j)) mp[j][j+n]=mp[j+n][j]=0; floyd(); for(int j=0;j<n;j++) if(i&(1<<j)) mp[j][j+n]=mp[j+n][j]=INF; } } return ans; } };
问你方案数。(950分) 题解:方案数比较蛋疼。开始dp[i][j]代表前i个节点,且第i个节点图第j种颜色的方案数。 dp[i][j]+=dp[i-1][k](没有从i节点出发的边) dp[i][j]+=(dp[p][k])(k!=j)(从i节点出发的边) 结果发现少了好多,最后在N=3,K=2研究了下,发现递推的方程应该在第i-1的地方转移。
#include <iostream> #include <cstring> #include <deque> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <list> #include <map> #include <set> #include <string> #include <vector> #include <cstdio> #include <functional> #include <algorithm> typedef long long LL; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int,int>pil; const int mod=1e9+7; LL dp[110][5]; class ColorfulLineGraphsDiv2 { public: int countWays( int N, int K ) { CLEAR(dp,0); for(int i=0;i<K;i++) dp[1][i]=1; for(int i=2;i<=N;i++) { for(int j=0;j<K;j++) { for(int k=0;k<K;k++) dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;//不连线 for(int p=1;p<i;p++) { for(int kk=0;kk<K;kk++) if(kk!=j) dp[i][j]=(dp[i][j]+dp[i-1][kk])%mod; } } } LL ans=0; for(int i=0;i<K;i++) ans=(ans+dp[N][i])%mod; return ans; } }; |
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/46495755