17
4
A man lives in the north-west corner (0, 0)
of a town with height h
and width w
. Everyday he walks from his home to the border (?, w)
or (h, ?)
. In the following example, the man goes to (3, 3)
today.
(0, 0) +--+ + + . (0, 4)
|
+ +--+--+ .
|
+ + + + .
|
(3, 0) . . . . . (3, 4)
The man records a bit at each points (+
in example above). Every time he reaches a point, he goes east if the bit is 1
and south otherwise. The bit is flipped after he leaves. For example:
Day 1: 1--0 1 1 Day 2: 0 1 1 1 Day 3: 1--1--1--1-- Day 4: 0 0 0 0
| | |
0 1--0 0 0 0 1 0 1 0 1 0 1--0 1 0
| | |
1 0 1--0 1--0 0 1 0 1 0 1 0 1--0 1
| | |
Destination: (3, 3) Destination: (3, 1) Destination: (0, 4) Destination: (3, 2)
Given the size of the town and the man's record, calculate the man's destination after n
days.
Input:
In the first line are three integers, h
, w
and n
.
In the following h
lines are w
integers, denoting the man's record.
h <= 1000, w <= 1000, n <= 1000000000
Output:
Two integers, denoting the man's destination after n
days.
Sample Input:
3 4 3
1 0 1 1
0 1 0 0
1 0 1 0
Sample Output:
0 4
Sample Code:
#include <iostream>
using namespace std;
bool d[1000][1000];
int main(){
int h, w, n;
cin >> h >> w >> n;
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
cin >> d[i][j];
int i, j;
while(n--)
for(i = 0, j = 0; i < h && j < w;){
bool &b = d[i][j];
d[i][j] ? j++ : i++;
b = !b;
}
cout << i << " " << j << endl;
}
Scoring:
- Lowest byte count in UTF-8 wins.
- If the running time of your code is independent of
n
, reduce your score by 50%.- Don't just calculate the results of all 1000000000 days or do anything similarly stupid to get this bonus. Find an efficient algorithm!
2 things I dont understand. The output, sometimes you use 0 index other times you dont. How does that work? Should it be like border+1? Second thing is the second line with scoring. How do you mean that? – Teun Pronk – 2014-04-04T09:58:16.617
Day 4 should output 3,2 right? – Teun Pronk – 2014-04-04T10:26:47.897
2If, no matter what
n
is, my code calculates the results of all 1000000000 days, then output the result ofn
, do I still get the -50% bonus? – user12205 – 2014-04-04T10:48:49.590@ace now you put it like that, it does make sense doesnt it? Thanks for that :P – Teun Pronk – 2014-04-04T10:59:41.057
@TeunPronk Yes. It's my fault. – johnchen902 – 2014-04-04T11:14:12.830
@ace Literally you do, but you'll definitely get my down-vote and I won't accept that as an answer. – johnchen902 – 2014-04-04T11:16:29.633
@johnchen902 aww, dont downvote my answer, ill change it after lunch. :) – Teun Pronk – 2014-04-04T11:18:49.483
And the cheeky answer is
print "0 0"
since he always returns home every day. – user80551 – 2014-04-04T11:55:10.903@johnchen902 deleted my answer for a bit, think thats better :P – Teun Pronk – 2014-04-04T12:24:50.883
@johnchen902: That's not very sporting, to punish somebody who finds a loophole in your rules. The better approach would be to fix the rules so there's no loophole. – Claudiu – 2014-04-05T00:58:28.587
@Claudiu I know. I did fix the loophole and I didn't down-vote anyone. That comment was just a fierce version of don't do that. – johnchen902 – 2014-04-05T01:01:35.810
Anyone who finds this scenario interesting might also like to read about Chris Langton's ant.
– jscs – 2014-07-29T21:28:25.090