Editing
2015 Feb Silver Problem 2 Cow Hopscotch (Silver)
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Code == === C++ === <pre> #include <iostream> #include <vector> #include <fstream> using namespace std; const int MOD = 1000000007; int main() { ifstream fin("barnjump.in"); ofstream fout("barnjump.out"); int rows, cols; fin >> rows >> cols; vector<vector<int>> grid(rows, vector<int>(cols)); for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols; ++col) { fin >> grid[row][col]; } } vector<vector<int>> dp(rows, vector<int>(cols, 0)); dp[0][0] = 1; for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols; ++col) { for (int nextRow = row + 1; nextRow < rows; ++nextRow) { for (int nextCol = col + 1; nextCol < cols; ++nextCol) { if (grid[row][col] != grid[nextRow][nextCol]) { dp[nextRow][nextCol] += dp[row][col]; dp[nextRow][nextCol] %= MOD; } } } } } fout << dp[rows - 1][cols - 1] << endl; return 0; } </pre> === Java === <pre> import java.io.*; import java.util.*; public class BarnJump { private static final int MOD = 1_000_000_007; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("barnjump.in")); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("barnjump.out"))); StringTokenizer st = new StringTokenizer(reader.readLine()); int rows = Integer.parseInt(st.nextToken()); int cols = Integer.parseInt(st.nextToken()); int[][] grid = new int[rows][cols]; for(int row = 0; row < rows; row++) { st = new StringTokenizer(reader.readLine()); for(int col = 0; col < cols; col++) { grid[row][col] = Integer.parseInt(st.nextToken()); } } int[][] dp = new int[rows][cols]; dp[0][0] = 1; for(int row = 0; row < rows; row++) { for(int col = 0; col < cols; col++) { for(int nextRow = row + 1; nextRow < rows; nextRow++) { for(int nextCol = col + 1; nextCol < cols; nextCol++) { if(grid[row][col] != grid[nextRow][nextCol]) { dp[nextRow][nextCol] += dp[row][col]; dp[nextRow][nextCol] %= MOD; } } } } } writer.println(dp[rows - 1][cols - 1]); writer.close(); } } </pre> === Python === <pre> MOD = 1000000007 def main(): with open('barnjump.in', 'r') as fin: rows, cols = map(int, fin.readline().split()) grid = [list(map(int, line.split())) for line in fin] dp = [[0] * cols for _ in range(rows)] dp[0][0] = 1 for row in range(rows): for col in range(cols): for next_row in range(row + 1, rows): for next_col in range(col + 1, cols): if grid[row][col] != grid[next_row][next_col]: dp[next_row][next_col] += dp[row][col] dp[next_row][next_col] %= MOD with open('barnjump.out', 'w') as fout: fout.write(f"{dp[rows - 1][cols - 1]}\n") if __name__ == "__main__": main() </pre> [[Category:Yearly_2014_2015]] [[Category:Silver]] [[Category:2D Array]] [[Category:Dynamic Programming]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information