Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- static int myId = -1;
- static void err(String s) {
- System.err.println("thread " + myId + ": " + s);
- }
- static void out(String s) {
- System.out.println(s);
- }
- static void out(long s) {
- System.out.println(s);
- }
- final static int M = 10;
- static char get(int i, int j) {
- if (j < 0 || j >= w) {
- return '#';
- }
- if (i >= h) {
- return '0';
- }
- return asteroids.GetPosition(i, j);
- }
- static int h, w;
- public static void main(String[] args) {
- // long START = System.currentTimeMillis();
- myId = message.MyNodeId();
- int nodes = message.NumberOfNodes();
- h = (int) asteroids.GetHeight();
- w = (int) asteroids.GetWidth();
- int len = Math.max(M, 1 + (w - 1) / nodes);
- int left = Math.min(w, len * myId);
- int right = Math.min(w, len * (myId + 1));
- int[] dp = new int[2 * M + (right - left)];
- Arrays.fill(dp, Integer.MIN_VALUE);
- char[] currentRow = new char[dp.length];
- char[] nextRow = new char[dp.length];
- int[] nextDp = new int[dp.length];
- Arrays.fill(currentRow, '#');
- for (int i = left - 1; i < right + 1; i++) {
- int pos = i - left + M;
- currentRow[pos] = get(0, i);
- if (currentRow[pos] != '#') {
- dp[pos] = currentRow[pos] - '0';
- }
- }
- int curLeft = left, curRight = right;
- for (int i = 1; i <= h; i++) {
- if (i % (M - 2) == 0) {
- if (myId != 0) {
- for (int j = 0; j < M; j++) {
- message.PutInt(myId - 1, dp[M - j - 1]);
- }
- message.Send(myId - 1);
- }
- if (myId != nodes - 1) {
- for (int j = 0; j < M; j++) {
- message.PutInt(myId + 1, dp[M + (right - left) + j]);
- }
- message.Send(myId + 1);
- }
- if (myId != 0) {
- message.Receive(myId - 1);
- for (int j = 0; j < M; j++) {
- dp[j + M] = Math.max(dp[j + M],
- message.GetInt(myId - 1));
- }
- }
- if (myId != nodes - 1) {
- message.Receive(myId + 1);
- for (int j = 0; j < M; j++) {
- dp[M + (right - left) - j - 1] = Math.max(dp[M
- + (right - left) - j - 1],
- message.GetInt(myId + 1));
- }
- }
- curLeft = left;
- curRight = right;
- }
- Arrays.fill(nextDp, Integer.MIN_VALUE);
- for (int j = curLeft - 2; j < curRight + 2; j++) {
- nextRow[j - left + M] = get(i, j);
- }
- for (int pos = curLeft; pos < curRight; pos++) {
- int p = pos - left + M;
- if (nextRow[p] != '#') {
- nextDp[p] = Math.max(nextDp[p], dp[p] + (nextRow[p] - '0'));
- }
- if (nextRow[p - 1] != '#' && currentRow[p - 1] != '#') {
- nextDp[p - 1] = Math.max(nextDp[p - 1], dp[p]
- + (currentRow[p - 1] - '0')
- + (nextRow[p - 1] - '0'));
- }
- if (nextRow[p + 1] != '#' && currentRow[p + 1] != '#') {
- nextDp[p + 1] = Math.max(nextDp[p + 1], dp[p]
- + (currentRow[p + 1] - '0')
- + (nextRow[p + 1] - '0'));
- }
- }
- curLeft--;
- curRight++;
- int[] tmp = dp;
- dp = nextDp;
- nextDp = tmp;
- char[] tmp2 = currentRow;
- currentRow = nextRow;
- nextRow = tmp2;
- }
- int result = Integer.MIN_VALUE;
- for (int i = 0; i < dp.length; i++) {
- result = Math.max(result, dp[i]);
- }
- message.PutInt(0, result);
- message.Send(0);
- if (myId == 0) {
- for (int i = 0; i < nodes; i++) {
- message.Receive(i);
- result = Math.max(result, message.GetInt(i));
- }
- if (result < 0) {
- result = -1;
- }
- out(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment