We need to count number of paths from (1,1) to (w,h) moving only east (increase i) or north (increase j) on a grid, with the restriction: a car that turns at an intersection cannot turn at the intersection immediately after that. In other words, you cannot have two consecutive turns. At each intersection, a turn is defined as a change of direction. Starting from (1,1), you have initial direction? The problem statement: "A car that turns at an intersection cannot turn at the intersection immediately after that." This means if you turned at some intersection (i.e., you entered the intersection from one direction and left in a different direction), then at the very next intersection you cannot turn again; you must go straight (i.e., continue in the same direction). However, you may turn later after at least one straight segment.
Interpretation: The path is a sequence of moves: east or north. A turn occurs at an intermediate vertex (intersection) where the direction of the move before the vertex differs from the direction after the vertex. Starting point (1,1) is not considered an intersection where you turn because you haven't entered from a previous move? Actually you start at (1,1), you have no incoming direction, so you can choose either east or north initially. Is that considered a turn? Typically not. The rule applies only after you have turned; so starting direction doesn't count as a turn. Also, at the destination (w,h), you stop, so no outgoing direction, so no turn considered.
Thus we count all monotonic paths from (1,1) to (w,h) (with w-1 east steps, h-1 north steps) such that there are no two consecutive turns.
We need number modulo 100000.
Constraints: w, h up to 100, datasets <=5. That's small, we could compute combinatorially with DP.
We need to count the number of paths with no two consecutive turns.
Let’s define the path as a sequence of moves (E or N). Path length = (w-1)+(h-1) steps. There are total steps count. The path can be represented by the sequence. A turn occurs at positions (internal vertices) where the previous move and next move differ.
We need number of sequences of length L = (w-1)+(h-1) consisting of E and N, with exactly w-1 E's and h-1 N's, such that there are no two consecutive turns. The turns are defined at positions 2..L? Actually at each internal vertex, which corresponds to after having taken some steps. More precisely, if we list moves m1, m2, ..., mL (each E or N), then for i from 1 to L-1, we have a turn at the i-th vertex (between step i and i+1) if m_i != m_{i+1}. So the condition "no two consecutive turns" means that there is no i such that both at vertex i and vertex i+1 we have turns. That is, we cannot have pattern m_i != m_{i+1} and m_{i+1} != m_{i+2} for any i in 1..L-2.
In other words, the sequence of moves cannot have three consecutive moves where the first and second differ, and the second and third differ. Since if the first two differ, that's a turn at the vertex after step i. If the second and third differ, that's a turn at the vertex after step i+1. So they must not both happen. So the only forbidden pattern is alternating moves of length 3? Actually if we have three moves: A, B, C, with A != B and B != C, then necessarily A = C? Let's check: if moves are from {E,N}. If A != B and B != C, then B is opposite of both A and C, so A and C must be the same (since there are only two directions). So the forbidden pattern is A, B, A (alternating) with A, B, A, i.e., two opposite moves around a central different move. That's exactly "turn, then immediate turn". Because after turning from A to B, you then turn from B to A, which is a turn again. So the condition forbids having an alternating pattern of length 3.
Thus the problem reduces to: Count number of sequences of length L with given numbers of E and N, such that it does not contain the pattern E N E or N E N as three consecutive moves.
But careful: Are there any restrictions regarding the initial direction? At start (before any move) there's no turn. So pattern starting at i=0? Not needed.
Also, the rule says: "A car that turns at an intersection cannot turn at the intersection immediately after that." So if you turn at an intersection, you must go straight through the next intersection. That means after a turn, the next step cannot be a turn. So indeed you cannot have two consecutive turns. So the forbidden pattern is exactly as described.
But is there any nuance about the start? Suppose you start by moving east, then north, then east: that's turn at first vertex (between first two moves: east->north is a turn) and also turn at second vertex (north->east). That's two consecutive turns, which is forbidden. So such path is not allowed.
If you start by moving north, then east, then north: similarly forbidden.
Thus all sequences that contain an alternating pattern of length 3 are forbidden.
Now we need to count sequences with given counts of E and N, avoiding the pattern ABA.
We can think of this as sequences with no two consecutive turns, meaning that between turns you must have at least two consecutive moves in the same direction (a straight segment of length at least 1? Actually a turn at a vertex means the move before and after are different. So if you have a turn, then the next move must be same as the previous move? Let's analyze: after a turn, at the next vertex, you cannot turn. So at the next vertex, the incoming and outgoing directions must be same. That means that after making a turn, you must continue straight for at least one step? More precisely: Suppose we have moves ... X, Y, Z ... with X != Y (turn at the vertex after X). Then the condition says you cannot turn at the vertex after Y, i.e., you must have Y == Z. So indeed after a turn, the next move must be the same as the move just made. So after a turn, you must have at least one straight move (the same direction as the turn's outgoing direction). So a turn is always followed by at least one move in the same direction.
Thus the allowed paths consist of "straight segments" of length at least 1. But the first segment can be any length >=1? Actually the starting direction is not constrained, so the first segment can be any positive length. And then if you decide to turn, you must have a segment of length at least 2? Let's think: A segment is a maximal sequence of consecutive same direction moves. The length of a segment is number of moves in that direction before you turn (or end). The turn occurs between segments. For a turn to be allowed, the segment after the turn must be of length at least 2? Actually after a turn, you cannot turn immediately, so the segment that follows a turn must have length at least 2? Wait: The condition is about the vertex after a turn: you cannot turn there. The turn occurs at the junction between segment A and segment B. The vertex immediately after the turn is the endpoint of the first move of segment B? Actually let's define: Sequence: a_1, a_2, ..., a_k (the moves). If we have segment lengths: segment1: L1 moves of direction d1, then a turn at the vertex after L1 moves (i.e., after move L1, we switch direction to d2). Then segment2 consists of L2 moves of direction d2. The condition: after the turn (i.e., at the vertex after the first move of segment2? Actually the turn itself is at the vertex where we change direction from d1 to d2. The rule says: "A car that turns at an intersection cannot turn at the intersection immediately after that." That means if we turned at intersection X (where we changed direction), then at intersection X+? Actually "immediately after that" means the next intersection you encounter after that turn. After turning, you travel along a road segment and reach the next intersection. At that next intersection, you cannot turn. So consider you turned at intersection I (the one you just entered from previous direction and left in new direction). Then you move one block (i.e., one move) to intersection I+? Actually you are at an intersection, you decide direction, then you go to the next intersection. So after turning, you go straight along the new direction for one block, and you arrive at the next intersection. At that intersection, you are not allowed to turn. So you must continue straight (i.e., the same direction) at that next intersection. So that means that after a turn, you must have at least two consecutive moves in the new direction? Actually the turn occurs at an intersection: you came from a direction d1 and left in d2 (d2 != d1). After leaving, you travel one block and arrive at the next intersection. At that next intersection, if you were to turn, you would need to change direction again. But the rule forbids that. So at that next intersection, you must continue in the same direction d2. That means you must have at least two moves in the direction d2 after the turn? Let's count: The turn occurs at the intersection between move i (incoming) and move i+1 (outgoing). At that intersection, you've just turned. Then you travel along move i+1, which is d2. Then you arrive at intersection after move i+1. That's the "immediately after" intersection. There, you must not turn, i.e., the next move (move i+2) must be in the same direction d2. So indeed, if a turn occurs at the transition from move i to move i+1, then we must have move i+1 == move i+2. That implies that the segment after a turn must have length at least 2. Because the first move of the new direction (i+1) is the block after the turn; you need at least a second move (i+2) in same direction to avoid a turn at the next intersection. But what about the end? If the path ends exactly after move i+1? That is, if you arrive at destination right after that move? The rule about not turning at the intersection after a turn applies only if there is a next intersection; if you have reached the destination, there is no further move, so no turn. So it's okay if after a turn you go one block and then reach the destination. So segment after a turn can be of length 1 only if that segment ends at the destination. In other words, a turn can be followed by a segment of length 1 only if that segment's last move reaches the target. Otherwise, if not at destination, you must have length at least 2.
Similarly, what about the starting segment? At the start, there is no prior turn, so you can have any positive length, including 1.
Thus allowed path is a sequence of alternating straight segments (all moves in same direction) of lengths L1, L2, L3, ..., Lk, where the directions alternate (since a turn changes direction, and you cannot have two turns consecutively, so you cannot have a segment of length 0, and you must alternate direction). So the directions will be: starting direction either east or north. If starting direction is east, then segments: east length L1, then north length L2, then east length L3, etc., or vice versa. And the condition: For each segment that is not the first and not the last? Actually the condition imposes: after a turn (i.e., after segment i, which ends with a turn to segment i+1), the segment i+1 must have length at least 2, unless it is the final segment (i.e., i+1 = k) and it ends at destination. Also, what about turning at the beginning? Not applicable.
Thus the counting becomes: number of ways to partition the required total east moves (w-1) and north moves (h-1) into alternating segments with lengths satisfying constraints. However, it's a bit messy due to the alternating direction constraint and the length constraints.
But with w,h ≤ 100, we can easily DP over positions and states representing the last direction and whether we are currently under a "forced straight" condition (i.e., after a turn we must go straight for at least one more move). But careful: The condition only prohibits a turn at the very next intersection. So if we have just turned, we are not allowed to turn at the next vertex. That can be captured by a state that remembers if the previous move was a turn or not. Actually easier: we can define DP based on the last two moves (or the direction of the last move and a flag indicating whether we are allowed to turn at the current position). Since the condition is about not having two consecutive turns, we can think of building the sequence step by step. At each step, we choose next move direction, subject to constraints on the count of E and N remaining, and also subject to: if the previous two moves constituted a turn (i.e., previous move was opposite to the move before it), then the next move must be same as previous (i.e., we cannot turn again). Conversely, if the previous two moves were not a turn (i.e., last two moves same), then we are free to choose either direction (subject to counts) because the previous vertex was not a turn, so turning now is allowed.
But careful: The condition is about not having two consecutive turns. A turn is defined at a vertex between two moves. So to decide if we can change direction at the upcoming vertex (i.e., between the last move we made and the next move), we need to know if the last vertex was a turn. The last vertex is the intersection before the last move? Actually if we are at an intersection after having completed some moves, we are about to decide the next move. The last vertex (the one we just came from) is the intersection between the previous move and the move before that? Let's define properly.
We are building the path step by step. Suppose we have already taken t moves, and we are currently at some intersection (i,j). The next move we will choose will take us to a new intersection. The decision whether we can turn (i.e., change direction) at the current intersection depends on whether the previous move (the one we just used to arrive) was itself a turn? Actually whether we arrived at the current intersection via a turn or not. The rule says: "A car that turns at an intersection cannot turn at the intersection immediately after that." So if we turned at the previous intersection (the one we left when we made the turn), then at this current intersection, we cannot turn. The previous intersection is the one we were at before making the last move. At that previous intersection, we turned if we changed direction there. That is, if the move before last (incoming direction to that previous intersection) and the last move (outgoing from that previous intersection) were different. So to know if we are currently at an intersection where turning is prohibited, we need to know if we turned at the previous intersection. That depends on the last two moves: if the last two moves were different, then we turned at the previous intersection, and thus we cannot turn at the current intersection. If the last two moves were the same, then we did not turn at the previous intersection, and we are free to turn now.
But there is also the initial situation: at start (1,1), there is no previous intersection, so there is no restriction; we can choose either direction. This can be modeled by assuming that the "last two moves" before start are something like "same direction" or we set a flag that we are allowed to turn. We can treat start as having not come from a turn, i.e., allowed to turn.
Thus DP state can be: (e, n, last_direction, turn_allowed_flag) or better: (e, n, last_direction, just_turned_flag) where just_turned_flag indicates whether the previous move was a turn (i.e., whether the last two moves were different). Actually we need to know whether we are currently under the restriction: i.e., we are at a vertex where turning is prohibited. That occurs if the last move was a turn (i.e., the previous vertex was a turn). That is equivalent to: the last two moves were different. Because if last two moves were different, then at the vertex between them we turned. After performing that move, we arrived at the current vertex, and we cannot turn here. So if last_two_different = true, then we cannot change direction now; we must continue straight (same direction as last move). If last_two_different = false (i.e., last two moves were same), then we are free to change or continue.
Thus DP state: (i, j, d, flag) where i = number of east moves made so far? Actually we have positions: current intersection (x,y) with x from 1..w, y from 1..h, but we can also use counts of east and north done. Since w,h <=100, we can DP over grid (100x100 = 10000) which is small. State includes last move direction d (E or N) and a boolean flag indicating whether the last move was preceded by a different direction? Actually flag = whether the previous vertex was a turn, i.e., whether the last two moves differ. But we can also derive that from just the last move and the previous state? However, it's easier to include a flag that indicates if we are forced to go straight. That flag is true if we turned at previous intersection; i.e., if we changed direction when we last moved. So we need to know if we are currently in a "just turned" state.
Alternative: Use DP with last direction and whether we are allowed to change direction now. But the allowedness depends on if we turned at the previous step. So we can store a flag "canChange" = (last_two_were_same) or "mustGoStraight" = (last_two_were_different). That flag can be computed from the previous state's info. When we transition, we decide the next direction nd. The condition is: if mustGoStraight (i.e., last_two_different), then nd must equal last_direction. Otherwise, nd can be either (subject to not exceeding grid boundaries). Also, if we choose nd != last_direction, that constitutes a turn at the current vertex (the vertex we are at now). But the rule only prohibits turning at the current vertex if the previous was a turn. So it's okay to turn as long as we are not forced straight. So that's fine.
Now, after we move with nd, we need to compute the new flag for the next state. The new flag (mustGoStraight for the next intersection) will be true if the move we just made (nd) is different from the last_direction (i.e., if we turned at the current vertex). Because after moving, we arrive at the next intersection, and the condition for that next intersection being prohibited to turn is if we turned at the current intersection. So new_flag = (nd != last_direction). Actually careful: The flag for the new state (the state after moving) should indicate whether we turned at the vertex we just passed, i.e., at the current intersection we are leaving? Let's define more concretely:
We are at intersection (x,y). The state includes: last_direction d (the direction we used to arrive at (x,y) from the previous intersection) and a flag f that indicates whether turning is prohibited at (x,y) because we turned at the previous intersection? Actually our earlier reasoning: The prohibition at the current intersection is based on whether the previous vertex was a turn. That is exactly f. So in state (x,y,d,f), f is true if we are at (x,y) and we turned at the previous intersection (i.e., the one we came from). Therefore, if f is true, we cannot change direction at this intersection. If f is false, we can.
Now we choose a direction nd to move to next intersection. We must ensure if f=true then nd == d (must go straight). If f=false, nd can be d (continue straight) or the opposite direction (turn). After moving to new intersection (x',y'), we will have a new state: the new last_direction is nd (the direction we just moved). And we need a new flag f' for that new intersection, which should be true if we turned at the intersection we just left (i.e., the current intersection (x,y)). That is, f' = (nd != d). Because if we changed direction, then we turned at (x,y). So f' = (nd != d). That matches.
Now initial state: At start (1,1). There is no incoming direction, so we don't have a last_direction and no previous turn. The rule does not restrict the first move. So we need to model initial states appropriately. We can think of starting with the ability to choose either east or north. In terms of our state, we could initialize by considering that before start, we have an imaginary previous move that is the same as whatever we choose? Or we can treat separately: from (1,1) we can go east or north. For the subsequent state, after moving, we will have a flag indicating whether we turned at (1,1). But since there was no incoming direction, turning at (1,1) is not defined (there is no previous direction to compare). Typically, we consider that at the starting intersection, you can't "turn" because there is no incoming direction. So moving east from (1,1) is not a turn regardless of previous. So the flag for the next intersection should be false because there was no turn at start. But we need to be consistent. So we can treat that from start, we can choose either direction, and after moving to the next intersection, we set f' = false (since we didn't turn, because there was no previous direction). That can be achieved by initializing DP for states after the first move with f = false. But then the DP recurrence for subsequent moves uses the rule.
Alternatively, we can define a state at intersection (x,y) that includes: direction of the last move (how we got here) and a boolean indicating if we turned at the previous intersection. At start, we don't have such a state because we haven't moved. We can instead start from (1,1) with a dummy direction and flag such that moving in any direction is allowed and sets appropriate flag. For simplicity, we can manually initialize DP for the positions after one move: from (1,1) we can go east to (2,1) and north to (1,2). For each, we set last_direction = the direction taken, and flag = false (since there was no turn at start). Then we continue DP from those states.
Thus we can do DP over grid with dimensions up to 100x100, and each state has 2 directions and 2 flags => 4 states per cell. That's 1001004 = 40k, trivial.
But we need to be careful about boundaries: We cannot go beyond w or h.
We need to compute number of ways to reach (w,h). At the destination, there is no restriction on the final move; we just need to end at (w,h). So any state that reaches (w,h) is valid regardless of its flag? But we must ensure that we don't turn after the last move? It doesn't matter because we stop. So we can sum over all states that land on (w,h) after moving? Actually our DP transitions: from a state at (x,y) we move to (x',y'). So when we arrive at (w,h), we consider that as a completed path. So we can accumulate counts when we transition to (w,h). Or we can let DP propagate and sum over states at (w,h) regardless of what would be next? But note that at the state at (w,h), we have arrived via some last direction and some flag. But there is no further move. So that state is a valid endpoint. So we can sum over all states at (w,h). But in our DP as described, we would be generating states at each intersection after moving there. So we can initialize dp[cell][d][f] as number of ways to reach that cell with given last direction and flag. Then final answer is sum over all d,f of dp[(w,h)][d][f].
But we have to be careful: At the start, we have dp at (1,1) undefined because no last direction. We could set dp at (1,1) with dummy state. But easier: initialize dp for cells (2,1) and (1,2) as described. Then DP runs for all cells. But we also need to include the possibility that w=1 or h=1? But constraints say w,h>=2. So at least 2x2 grid. So start and end are distinct.
So algorithm: For each dataset, compute DP.
Define directions: 0 for east (increase x), 1 for north (increase y). Or maybe 0 east, 1 north.
We'll create array dp[x][y][d][f] (0-index or 1-index). Dimensions: w up to 100, h up to 100. Use 1-index for clarity.
Initialize all zero.
After first move: from (1,1) go east to (2,1): dp[2][1][0][0] += 1; go north to (1,2): dp[1][2][1][0] += 1.
Then iterate over all cells (x,y) from 1 to w, 1 to h, but careful order: Since moves only increase x or y, we can process in increasing x and y (topological order). For each cell (x,y), for each d (0/1), f (0/1), if dp[x][y][d][f] > 0, consider possible next moves.
At state (x,y,d,f), we are at (x,y) and we have arrived with last direction d, and flag f indicates whether we turned at previous intersection (i.e., whether turning is prohibited here). Allowed next direction nd must be either:
- If f == 1: we must go straight, so nd = d only.
- If f == 0: nd can be d (straight) or opposite direction (turn).
But also we must respect grid bounds: if nd=0 (east), then x+1 <= w; if nd=1 (north), then y+1 <= h.
Then compute next cell: (x2,y2) = (x+1,y) if nd=0; (x,y+1) if nd=1.
Next flag f2 = (nd != d) ? 1 : 0. Because if we turned at current intersection (nd != d), then at the next intersection turning is prohibited.
Add dp[x][y][d][f] to dp[x2][y2][nd][f2] (mod 100000).
At the end, answer = sum_{d=0,1} sum_{f=0,1} dp[w][h][d][f] mod MOD.
Let's test on sample: w=3, h=4. Let's compute manually.
We'll need to see if answer is 5.
First, enumerate all monotonic paths from (1,1) to (3,4). Total steps: (2 east, 3 north). Number of unrestricted paths = C(5,2)=10. Those with no two consecutive turns? Let's list all 10 and see which are forbidden.
Coordinates: (1,1) -> (3,4). Sequence of moves: E or N.
List all sequences with 2 E, 3 N:
- E E N N N
- E N E N N
- E N N E N
- E N N N E
- N E E N N
- N E N E N
- N E N N E
- N N E E N
- N N E N E
- N N N E E
Now detect forbidden patterns (consecutive turns). A turn occurs at positions where move changes. Let's compute.
-
EE NNN: moves: E,E,N,N,N. Changes: E->E (no turn), E->N (turn), N->N (no), N->N (no). Only one turn, okay.
-
E,N,E,N,N: moves: E,N,E,N,N. Changes: E->N (turn), N->E (turn) -> two consecutive turns? Let's check: After E->N turn at vertex after first move, then N->E turn at vertex after second move. That's consecutive turns -> forbidden.
-
E,N,N,E,N: changes: E->N (turn), N->N (no), N->E (turn). Turns at 1st and 3rd vertex are not consecutive (there is a straight in between). So allowed? But check: The vertices: after move 1 (E->N turn), after move 2 (N->N no), after move 3 (N->E turn). There is a vertex between move2 and move3 where we turned, but that vertex is not immediately after previous turn because there was a vertex where we didn't turn. So allowed. However, we also need to consider if after a turn we must go at least two straight? In this sequence: after turn (E->N), we moved N, then at next intersection we turned? Actually after turn at vertex after move1, we are at the intersection after one block north? Let's simulate: Start (1,1). Move1: E to (2,1). At (2,1) we came from west? Actually we started, we didn't have incoming. After first move, we are at (2,1). That's the first vertex after start. The turn would be considered only if we changed direction relative to previous move. Since there is no previous move, we cannot have a turn at start. So move1 is just initial direction. After move1, we are at (2,1). Then we move N as move2: that is a change from E to N, so at the vertex (2,1) we turned. So turn at (2,1). Then after moving N, we are at (2,2). At (2,2), because we turned at previous intersection, rule says cannot turn at this intersection. In sequence 3: move2: N, move3: N, so from (2,2) we move N again (since move3 is N). So we did NOT turn at (2,2). So rule satisfied. Then after move3: we are at (2,3). Then move4: E, which changes direction from N to E, so at (2,3) we turn. Was a turn allowed at (2,3)? At previous intersection (2,2), we did not turn (since we went straight). So allowed. So sequence 3 is allowed.
-
E N N N E: changes: E->N (turn), N->N (no), N->N (no), N->E (turn). Turns separated, allowed.
-
N E E N N: changes: N->E (turn), E->E (no), E->N (turn). Turn at vertices after move1 and after move3? Let's check: start N to (1,2). At (1,2) we turned? No previous, so first move not a turn. After move1 we are at (1,2). Then move2: E, change from N to E => turn at (1,2). Then move2 to (2,2). Then move3: E (straight) => at (2,2) we didn't turn. Then move4: N, change from E to N => turn at (2,2)?? Wait: we need to check vertices: after move2, we are at (2,2). The turn at (1,2) was after move1. At (2,2), we made move3: E (straight) so no turn. Then after move3, at (3,2), we make move4: N (change) => turn at (3,2)?? Actually the vertex where we change is at (3,2) after move3? Let's be systematic: Define vertices as the intersections after each move. The turn occurs at the intersection before making the next move. So after move1 (N) we are at (1,2). Then we decide move2 (E) - that decision is made at (1,2). If the previous move was N and we choose E, that's a turn at (1,2). After move2, we are at (2,2). Then we decide move3 (E) at (2,2): previous move was E, so we go straight, no turn. After move3, we are at (3,2). Then we decide move4 (N) at (3,2): previous move was E, now N -> turn at (3,2). So turns at (1,2) and (3,2). They are not consecutive vertices because there is (2,2) between them where we didn't turn. So allowed? But check the rule: "A car that turns at an intersection cannot turn at the intersection immediately after that." The intersection immediately after turning at (1,2) is (2,2). At (2,2) we did not turn. So okay. So sequence 5 is allowed.
So allowed paths: 1,3,4,5,8,10? Let's count: 1,3,4,5,8,10 = 6? That's 6, but expected answer is 5. Did we miscount? Let's re-evaluate each:
1: EE NNN -> allowed.
3: E N N E N -> allowed? Let's double-check turns: start (1,1) move1 E to (2,1); at (2,1) choose N => turn; move2 N to (2,2); at (2,2) choose N => straight (no turn); move3 N to (2,3); at (2,3) choose E => turn; move4 E to (3,3); at (3,3) choose N => turn? Wait we have moves: E, N, N, E, N. That's 5 moves (since 2E+3N). Sequence: move1 E, move2 N, move3 N, move4 E, move5 N. After move4 (E) we are at (3,3). At (3,3) we have to choose move5 N. The previous move was E, and we choose N, so that's a turn at (3,3). But we need to check if that turn is allowed. The previous intersection before (3,3) is (2,3) where we turned? Let's step by step:
- Start (1,1)
- Move1 E: to (2,1). At (2,1): we came from start, no turn.
- Move2 N: from (2,1) to (2,2). Decision at (2,1) was N, which is a change from previous move E => turn at (2,1). So after move2, we arrive at (2,2). At (2,2), because we turned at previous intersection (2,1), we cannot turn.
- Move3 N: from (2,2) to (2,3). Decision at (2,2): we go N (same as previous direction N). So no turn. Good.
- Move4 E: from (2,3) to (3,3). Decision at (2,3): previous move was N, we choose E => turn at (2,3). So after move4, we arrive at (3,3). At (3,3), because we turned at previous intersection (2,3), we cannot turn.
- Move5 N: from (3,3) to (3,4). Decision at (3,3): we choose N. But previous move was E, so N != E => that would be a turn at (3,3). However, rule says at (3,3) we cannot turn because we turned at previous intersection (2,3). So this move5 N is not allowed! Indeed, after a turn at (2,3), we must go straight at the next intersection (3,3). But we are turning, which violates the rule. Therefore sequence 3 is NOT allowed. Our earlier analysis missed that. So 3 is forbidden.
Now check 4: E N N N E. Sequence: E, N, N, N, E.
- Move1 E: (2,1)
- Move2 N: turn at (2,1) -> arrive (2,2) (cannot turn at (2,2))
- Move3 N: straight at (2,2) -> arrive (2,3) (now allowed to turn, because at (2,2) we didn't turn)
- Move4 N: straight at (2,3) -> arrive (2,4) (allowed to turn? at (2,3) we didn't turn, so fine)
- Move5 E: at (2,4), previous move N, choose E -> turn at (2,4). This is allowed because at previous intersection (2,3) we did not turn. So sequence 4 is allowed.
But check: after move5, we arrive at (3,4) destination, no further moves. So okay.
Now 5: N E E N N.
Sequence: N, E, E, N, N.
- Move1 N: (1,2)
- Move2 E: turn at (1,2) -> arrive (2,2) (cannot turn)
- Move3 E: straight at (2,2) -> arrive (3,2) (allowed to turn)
- Move4 N: at (3,2) previous E, choose N -> turn at (3,2) -> arrive (3,3) (cannot turn)
- Move5 N: at (3,3) must go straight (since cannot turn) -> choose N (straight) -> arrive (3,4) destination. So allowed? Check: at (3,3) we had to go straight because we turned at (3,2). We did go straight (N) which matches previous direction N? Wait previous direction after move4 was N (since we turned to N). So at (3,3) we choose N (same) -> straight, allowed. So 5 is allowed.
8: N N E E N.
Sequence: N, N, E, E, N.
- Move1 N: (1,2)
- Move2 N: straight at (1,2) -> (1,3) (allowed to turn)
- Move3 E: turn at (1,3) -> (2,3) (cannot turn)
- Move4 E: straight at (2,3) -> (3,3) (allowed to turn)
- Move5 N: at (3,3) previous E, choose N -> turn at (3,3) -> (3,4) dest. But wait: at (3,3) we are allowed to turn because previous intersection (2,3) was straight? Actually after move4 we arrived at (3,3). The turn at (1,3) was earlier; we need to check: after move3 we turned at (1,3) -> at (2,3) we cannot turn. We then went straight at (2,3) (move4), so at (2,3) no turn. Then we arrived at (3,3). So at (3,3), the previous intersection (2,3) did not have a turn, so we are allowed to turn. So yes, allowed. So 8 is allowed.
10: N N N E E.
Sequence: N,N,N,E,E.
- Move1 N: (1,2)
- Move2 N: straight -> (1,3)
- Move3 N: straight -> (1,4)
- Move4 E: turn at (1,4) -> (2,4) (cannot turn)
- Move5 E: straight at (2,4) -> (3,4) dest. allowed.
Thus allowed: 1,4,5,8,10 = 5 paths. Yes matches sample output 5.
Thus our DP should produce 5.
Now test on 15x15 sample output 43688.
We need modulo 100000.
DP should be straightforward.
But we might need to ensure we handle the initial moves correctly. In our DP initialization we set dp[2][1][0][0]=1 and dp[1][2][1][0]=1. Then we process all cells in increasing order of x and y? Since moves only increase coordinates, we can process in loops for x from 1 to w, y from 1 to h, and when at (x,y), we propagate to larger coordinates. But careful: When we are at (x,y), we might have dp values from earlier states, because we only propagate forward. So we can iterate x=1..w, y=1..h, and for each state, we propagate to east (x+1,y) and north (x,y+1) accordingly. But we need to ensure we don't use dp values that are updated later? Since we are only adding to cells with larger coordinates, if we process x and y increasing, then when we process (x,y), the target cells (x+1,y) or (x,y+1) are not yet processed in the loops, which is fine because we are building DP forward. So order: for x from 1 to w, for y from 1 to h: process states at (x,y). But note: both x and y increase? Actually if we iterate y from 1 to h inside x from 1 to w, that will process some cells after others? Example: x=1, y=1 first; then x=1, y=2; then x=1, y=3... then x=2, y=1. But cell (2,1) gets contributions from (1,1) when we process (1,1). However, if we process (1,1) first, then we need to have dp[2][1] initialized? Our initialization set dp[2][1] directly, but we also will get contributions from (1,1) via the DP propagation? Actually we purposely did not include dp at (1,1) because we started from after first move. But if we want to incorporate the initial moves into the DP, we could start from (1,1) with a special state. Let's design systematically.
Option 1: Include start state at (1,1) with a dummy direction and flag that allows both moves and sets flag false after moving. For example, we could define a state at (1,1) with last_direction undefined, but we can treat as: at (1,1), we are allowed to move in either direction, and the resulting flag is always false. This can be done by initializing dp for (1,1) with a special state? Alternatively, we can handle the first step manually as we described.
Option 2: Use DP that includes the start state with no last direction and a flag that indicates we are at start. Then for each move from start, we set the flag appropriately. But easier is to do manual initialization.
Let's proceed with manual initialization. We'll set dp[1][1] not used; we'll set dp[2][1][0][0]=1, dp[1][2][1][0]=1. Then we will iterate over all cells (x,y) from 1 to w, y from 1 to h, but skip those that are never reached? Actually we should process all cells because dp may be set for any cell that can be reached. However, note that our initial states are at (2,1) and (1,2). So we need to process (2,1) and (1,2) and propagate from there. But also there might be other cells reachable from those, etc. So we should process cells in order of increasing (x+y) or just nested loops with x from 1..w, y from 1..h. But careful: If we iterate x from 1 to w, and for each x, y from 1 to h, then when we are at (1,2) (x=1, y=2), we will process it after processing (1,1) and maybe (1,2) after (1,1)? Actually order: for x=1, y=1 (skipped because dp all zero), then y=2 (we have dp[1][2] initialized), so we process it. That's fine. For x=2, y=1, we have dp[2][1] initialized; we will process when x=2, y=1. And when we process (1,2), we might add to (2,2) or (1,3). Those target cells may not have been processed yet, which is okay. So it works.
But we must be careful: When we process (2,1), we might add to (2,2) and (3,1). That's fine.
One subtlety: The DP state includes direction and flag. But at (2,1), we have dp[2][1][0][0]=1. That means we arrived at (2,1) via east, and the flag is 0 (meaning we are allowed to turn at (2,1) because we did not turn at previous intersection (the start)). Indeed, at start we didn't turn, so allowed to turn at (2,1). Good.
At (1,2), dp[1][2][1][0]=1: arrived via north, flag 0.
Now processing (2,1): f=0, d=0 (east). Allowed moves: nd can be 0 (east) or 1 (north) because f=0. Check bounds: if nd=0 (east), then x+1 <= w? w=3 so (3,1) valid. If nd=1 (north), then (2,2) valid. For each, compute f2 = (nd != d)? For nd=0: f2=0; for nd=1: f2=1. So we add 1 to dp[3][1][0][0] and dp[2][2][1][1].
Similarly processing (1,2): d=1 (north), f=0. Allowed nd=1 (north) or 0 (east). For nd=1: (1,3) valid? h=4 so yes; f2=0. For nd=0: (2,2) valid; f2=1 (since nd != d). So add to dp[1][3][1][0] and dp[2][2][0][1].
Now continue. Let's compute for w=3,h=4 manually to verify answer 5.
We'll compute DP up to (3,4). We'll track states.
Initialize:
(2,1): (0,0):1
(1,2): (1,0):1
Now iterate all cells in order x=1..3, y=1..4.
We'll maintain a table. We'll list dp[x][y][d][f] as (d,f): count.
Start with all zero.
After init:
(2,1): (0,0)=1
(1,2): (1,0)=1
Now process (1,1): no states -> ignore.
(1,2): we have (1,0)=1. Process:
- nd=1 (north): to (1,3). f2=0 -> add to (1,3)[1,0] +=1
- nd=0 (east): to (2,2). f2=1 -> add to (2,2)[0,1] +=1
After processing, we could update dp. So now:
(1,3): (1,0)=1
(2,2): (0,1)=1
Now (1,3): later we will process.
(1,4): not yet.
Next (1,3) when we get to it (x=1,y=3). At that time dp[1][3][1,0]=1.
But we need to continue loops. After (1,2), next y=3? Actually loop: for x=1, y from 1 to 4. We already did y=1,2. Next y=3. So process (1,3) now.
Process (1,3): state (1,0)=1. d=1 (north), f=0.
- nd=1 (north): to (1,4). f2=0 -> (1,4)[1,0] +=1
- nd=0 (east): to (2,3). f2=1 -> (2,3)[0,1] +=1
Now (1,4): later.
Now y=4: (1,4) currently has maybe some states. Currently we have (1,4)[1,0]=1 from above. But also later may get more contributions from other cells? But we are processing x=1, y=4 now? Since we are in the outer loop for x=1, we have y=4 now. At this moment, we have already set (1,4)[1,0]=1 from (1,3) processing, but we haven't processed (1,4) yet because we are iterating y sequentially. So we will process (1,4) after we have processed (1,3) and before moving to x=2. However, note that there might be contributions to (1,4) from other cells like (2,4)? No, because those would have x>1 and haven't been processed yet. So at the moment of processing (1,4) within x=1 loop, we only have whatever contributions have been added so far from earlier cells in the same x=1 column (which are (1,3) and maybe (1,2) but (1,2) didn't go to (1,4) directly). So it's safe to process (1,4) now. But careful: The DP propagation from (1,4) will go to east and north. Since we are processing in increasing x and y, when we process (1,4), its target cells (2,4) and (1,5) (out of bounds) will be later. That's fine.
Process (1,4): d=1,f=0, count=1.
- nd=1 (north): y+1=5>h=4 so invalid.
- nd=0 (east): to (2,4). f2=1 (since nd!=d). So add to (2,4)[0,1] +=1.
Now done with x=1.
Now x=2, y from 1 to 4.
First (2,1): state (0,0)=1. Process:
- nd=0 (east): to (3,1). f2=0 -> (3,1)[0,0] +=1
- nd=1 (north): to (2,2). f2=1 -> (2,2)[1,1] +=1 (note: add to existing (2,2)[1,1] if any; currently (2,2) has (0,1)=1 from earlier, and maybe others).
Now (2,2): we have existing states:
(0,1)=1 from (1,2) earlier,
(1,1)=1 from (2,1) just added? Actually we just added (2,2)[1,1] +=1, so now (2,2) has (0,1)=1 and (1,1)=1.
But also we might get contributions later from other cells, but we are processing (2,2) now (x=2,y=2). So we need to process these states now. Let's list exactly after processing (2,1) we have:
(3,1): (0,0)=1
(2,2): (0,1)=1, (1,1)=1
Now also from earlier we had (2,2) maybe also from other? Only from (1,2) and (2,1) so far. Good.
Now process (2,2). We'll process each state.
First state: d=0,f=1 count=1. f=1 means we must go straight: nd = d =0 only. Check bounds: east to (3,2) valid. f2 = (nd != d) ? 0 -> but nd==d so f2=0. So add to (3,2)[0,0] +=1.
Second state: d=1,f=1 count=1. f=1 => must go straight: nd = d =1 only. Check north to (2,3) valid. f2=0. Add to (2,3)[1,0] +=1.
Now (2,3): currently we have (2,3)[0,1]=1 from (1,3) earlier, and now we add (2,3)[1,0]=1.
Also note: (2,2) might get more contributions later from other cells? But we are processing it now, so any contributions that come later (from (1,2) already accounted, (2,1) accounted, maybe from (1,2) we already accounted; but (1,2) gave to (2,2) earlier, and we already included that. There is also possibility of contributions from (1,2) later? No. So (2,2) is done.
Now proceed to (2,3) (x=2,y=3). At this point, we have two states: (0,1)=1 and (1,0)=1.
But before processing (2,3), we need to see if any other contributions might arrive later? For example, from (2,2) we just processed added to (2,3). That's included. From (1,3) we already added (2,3)[0,1]=1. Could there be from (1,3) also? Already accounted. From (2,2) only. Also from (1,3) we accounted. So (2,3) is complete for now? Possibly from (2,2) we gave (2,3)[1,0]=1. So we have both.
Now process (2,3). We'll process each state.
State (d=0,f=1) count=1. f=1 => must go straight nd=0. Check east: (3,3) valid. f2=0. Add to (3,3)[0,0] +=1.
State (d=1,f=0) count=1. f=0 => can choose nd=1 or 0. Check bounds:
- nd=1 (north): to (2,4) valid. f2 = (nd != d) ? 0? Actually d=1, nd=1 -> f2=0. So add to (2,4)[1,0] +=1.
- nd=0 (east): to (3,3) valid. f2 = (nd != d) ? 1? Since 0 != 1, f2=1. So add to (3,3)[0,1] +=1.
Now (2,4): currently we have (2,4)[0,1]=1 from (1,4) earlier, and now we add (2,4)[1,0]=1 from this state.
Also (3,3): we have (3,3)[0,0] =1 (from first state) and (3,3)[0,1]=1 (from second state).
Now (2,4) is next in loop (x=2,y=4). We'll process after finishing (2,3). But we still need to process (2,4). But note that (2,4) might also get contributions from other cells like (2,3) we just processed, and from (1,4) we already gave. Also from (2,3) we added (2,4)[1,0] which we have. Could there be from (3,4)? No. So process (2,4) now.
Now (2,4) states: (0,1)=1, (1,0)=1.
Process each:
State (d=0,f=1) count=1. f=1 => must go straight nd=0. East to (3,4) valid (since w=3). f2=0. Add to (3,4)[0,0] +=1.
State (d=1,f=0) count=1. f=0 => can choose nd=1 or 0. Bounds:
- nd=1: north to (2,5) invalid (h=4).
- nd=0: east to (3,4) valid. f2 = (0 != 1) -> 1. Add to (3,4)[0,1] +=1.
Now (3,4): we have (0,0)=1 and (0,1)=1 so far.
Now we still have to process cells with x=3.
First (3,1): we have (0,0)=1 from (2,1) earlier.
Process (3,1): d=0,f=0 count=1.
Allowed nd: 0 (east) -> x+1=4>w invalid; nd=1 (north) -> to (3,2) valid. f2 = (1 != 0) -> 1. Add to (3,2)[1,1] +=1.
Now (3,2): we already have (3,2)[0,0]=1 from (2,2) first state? Wait, earlier from (2,2) state (0,1) we added to (3,2)[0,0]=1. Yes, we have (3,2)[0,0]=1. Now we also get (3,2)[1,1]=1 from (3,1). So (3,2) states: (0,0)=1, (1,1)=1.
Process (3,2) now.
First state (d=0,f=0) count=1. f=0 => can choose nd=0 or 1.
- nd=0 (east): to (4,2) invalid (w=3).
- nd=1 (north): to (3,3) valid. f2 = (1 != 0) -> 1. Add to (3,3)[1,1] +=1.
Second state (d=1,f=1) count=1. f=1 => must go straight nd=1. North to (3,3) valid. f2=0 (since nd == d). Add to (3,3)[1,0] +=1.
Now (3,3): existing counts: (0,0)=1, (0,1)=1. Now we add (1,1)=1 and (1,0)=1.
So (3,3) now has:
(0,0)=1
(0,1)=1
(1,0)=1
(1,1)=1
Process (3,3) now.
State (0,0) count=1. f=0 => can choose nd=0 or 1.
- nd=0 (east): to (4,3) invalid.
- nd=1 (north): to (3,4) valid. f2 = (1 != 0) -> 1. Add to (3,4)[1,1] +=1.
State (0,1) count=1. f=1 => must go straight nd=0. East invalid. So no move? Actually nd=0 is invalid because w=3, cannot go east. So this state contributes nothing to destination. But also could go north? Not allowed because must go straight. So no.
State (1,0) count=1. f=0 => can choose nd=1 or 0.
- nd=1 (north): to (3,4) valid. f2 = (1 == 1) -> 0. Add to (3,4)[1,0] +=1.
- nd=0 (east): invalid.
State (1,1) count=1. f=1 => must go straight nd=1. North to (3,4) valid. f2 = (nd==d) -> 0. Add to (3,4)[1,0] +=1.
Now update (3,4) counts: previously we had (0,0)=1, (0,1)=1. Now we add (1,1)=1, (1,0)=1 from state (0,0) gave (1,1)? Actually (0,0) gave (3,4)[1,1] +=1. (1,0) gave (3,4)[1,0] +=1. (1,1) gave (3,4)[1,0] +=1.
So after adding:
(0,0)=1
(0,1)=1
(1,0)=1 (from (1,0) state) +1 (from (1,1) state) =2? Actually we need to sum properly.
Let's recompute (3,4) final totals:
Initial from (2,4):
- from (2,4) state (0,1) gave (3,4)[0,0] +=1.
- from (2,4) state (1,0) gave (3,4)[0,1] +=1.
So after (2,4): (0,0)=1, (0,1)=1.
From (3,3) states:
- (0,0) gave (3,4)[1,1] +=1 => (1,1)=1.
- (0,1) gave nothing.
- (1,0) gave (3,4)[1,0] +=1 => (1,0)=1.
- (1,1) gave (3,4)[1,0] +=1 => add another to (1,0): becomes 2.
So final (3,4): (0,0)=1, (0,1)=1, (1,0)=2, (1,1)=1.
Now total paths = sum = 1+1+2+1 = 5. Good.
So DP works.
Now we need to compute modulo 100000. Since numbers can be large but w,h up to 100, total number of paths without restrictions can be huge (C(198,99) ~ 2e58) but modulo 100000, we can handle with DP and modulo at each step.
Complexity: O(wh4*2) = O(wh). Fine.
Now think about potential pitfalls: The DP we described seems correct. But we must ensure we correctly handle the initial step. In the DP we manually seeded two states. Alternatively, we could treat start as a state with a special direction and flag that ensures both moves and sets flag false. Another way: set dp[1][1] with dummy state? We could create a state with f=0 and d undefined? Or we could allow a move from start with a special condition that after moving, f2=0 always. That can be done by initializing dp[1][1][?]? Actually we could add a pseudo direction and f such that from start we can move east or north and produce f=0. For example, we could set dp[1][1][0][0] = 1 and dp[1][1][1][0] = 1? But that would mean we are at (1,1) and we have arrived with direction 0 or 1, which is not true. But if we then propagate from those states, the move from (1,1) with d=0 would mean we came from west? That would be impossible because west of (1,1) is out of bounds. However, we could consider that the first move is a continuation of an imaginary previous move same as the chosen direction, so that we don't turn. That would yield f2=0. But using such pseudo states may overcount? Let's test: if we set dp[1][1][0][0]=1, then from (1,1) with d=0,f=0 we can move east (nd=0) -> f2=0, or north (nd=1) -> f2=1 (since nd != d). That would give for north move f2=1, meaning after moving north, we would have a flag indicating we turned at start, which is not correct because at start there is no turn. So that would incorrectly produce a restriction on the next intersection after a north initial move. Indeed, starting by going north is not a turn, so after that first move, you should be allowed to turn at the next intersection. Our manual initialization set f2=0 for both initial moves. So if we used pseudo states, we would need to treat start specially: from start, any move should set f2=0 regardless of d and nd. So easiest is to just manually initialize.
Thus we will implement as described.
Edge Cases: w=2, h=2. Let's manually compute. Paths from (1,1) to (2,2) have two moves: either E then N, or N then E. Both have a turn at the only interior vertex? Actually (1,1) -> (2,1) -> (2,2): sequence: E,N. At the vertex (2,1), we turned from E to N. That's a turn. But there is no next intersection after that turn because after moving N we arrive at (2,2) destination. So both paths are allowed. So answer should be 2. Let's test DP: w=2,h=2. Initialize: (2,1)[0,0]=1, (1,2)[1,0]=1. Process (2,1): d=0,f=0: can move nd=0? east to (3,1) invalid; nd=1 north to (2,2): f2=1? Actually nd=1 != d=0 so f2=1. So dp[2,2][1,1] +=1. Process (1,2): d=1,f=0: nd=1 north invalid; nd=0 east to (2,2): f2=1 (since nd=0 != d=1). So dp[2,2][0,1] +=1. Sum = 2. All flags f=1 but that's okay, we sum all. So answer 2.
Now w=2, h=3. Paths from (1,1) to (2,3). Steps: one E, two N. Sequences: E N N, N E N, N N E. Check restrictions:
- E N N: moves: E,N,N. Turn at (2,1)? after E, go N -> turn; then at (2,2) go N straight; destination (2,3). Allowed.
- N E N: moves: N,E,N. Turn at (1,2) N->E turn; then at (2,2) E->N turn? After turn at (1,2), we go to (2,2) and then choose N, which is a turn at (2,2) if we change from E to N. That's consecutive turns? The previous turn was at (1,2). The next intersection is (2,2). So two consecutive turns -> forbidden.
- N N E: moves: N,N,E. Turn at (1,3) N->E turn; destination after that. Allowed.
So allowed 2 paths. Let's DP compute: w=2,h=3. We'll quickly test mental? It should yield 2.
Our DP should work.
Now for w=3, h=2 (symmetric) should also be 2? Let's see: Steps: 2E,1N. Sequences: E E N, E N E, N E E. E E N: turn at (3,1)?? Actually path: (1,1)->(2,1)->(3,1)->(3,2): moves E,E,N. Turn at (3,1) from E to N. That's only one turn, allowed. E N E: moves: E,N,E. Turn at (2,1) (E->N) and at (2,2) (N->E) consecutive -> forbidden. N E E: moves: N,E,E. Turn at (1,2) (N->E), then straight, allowed. So allowed 2. So symmetric.
Now larger grids.
Thus DP approach is valid.
But before coding, consider whether there is any nuance about the rule "A car that turns at an intersection cannot turn at the intersection immediately after that." Could it also apply if you turn at the first intersection? According to our interpretation, after turning at the first turn, you cannot turn at the next intersection. That's covered.
One more nuance: What about the case where you turn at an intersection and then the next intersection is the destination? If you turn and then in the next move you go straight to destination, that's fine because there's no turn at the destination. But what if after a turn you go one block and then at the next intersection you would need to turn to reach destination? But if that next intersection is the destination, turning is not an action; you stop. So it's allowed. Our DP handles because when we are at a state and we move to destination (w,h), we don't need to check anything beyond. The flag f2 computed is irrelevant because we stop. So we just add to counts at (w,h). So it's fine.
Now modulo 100000. Since we are only asked for remainder, we can take mod after each addition.
Implementation details: We'll read lines until "0 0". For each dataset, compute answer.
We'll use a 4D array: dp[w+1][h+1][2][2] maybe as int. Since w,h up to 100, dp size 1011012*2 = ~40k, fine. However, we need to ensure we zero it properly each dataset.
We'll initialize all to 0. Then set:
if w>=2: dp[2][1][0][0] = 1;
if h>=2: dp[1][2][1][0] = 1;
But careful: w could be 2, h=2 etc. Works.
Then loops:
for x = 1 to w:
for y = 1 to h:
for d in {0,1}:
for f in {0,1}:
val = dp[x][y][d][f];
if val == 0: continue;
// determine allowed next directions
if f == 1:
// must go straight: only d
nd_list = [d]
else:
// can go straight or turn: both directions
nd_list = [0,1] // but need to check bounds and also cannot go out of grid
for nd in nd_list:
// check grid bounds
if nd == 0: // east
if x == w: continue; // cannot go east
nx = x+1; ny = y;
else: // north
if y == h: continue;
nx = x; ny = y+1;
// compute new flag
nf = 1 if nd != d else 0;
// add val to dp[nx][ny][nd][nf] modulo MOD
dp[nx][ny][nd][nf] = (dp[nx][ny][nd][nf] + val) % MOD
After loops, answer = sum over d,f of dp[w][h][d][f] % MOD.
But we must be careful: In the loops, we also process the start cell (1,1). But we haven't initialized dp[1][1] with any state, so it's zero and will be skipped. That's fine. However, we also will process cells like (2,1) etc.
But there is a subtle issue: When we process a cell, we might add contributions to cells with larger coordinates that we haven't processed yet. That's fine because we are doing forward DP. However, if we process cells in order x from 1..w, y from 1..h, we guarantee that when we are at (x,y), all contributions to (x,y) have already been made? Not necessarily. Because contributions to (x,y) can come from (x-1,y) (west) and (x,y-1) (south). If we process x increasing and y increasing, then when we process (x,y), we have already processed (x-1,y) (since x-1 < x, and we have looped over all y for that x-1) and (x,y-1) (since for same x, we process y increasing). So both predecessor cells have been processed, and any contributions from them to (x,y) would have been added earlier in the loop. Good! This works because we propagate forward during the loops, but the order ensures that when we get to (x,y), its dp value is complete (from all possible incoming paths). Because all incoming moves come from cells with either lower x or lower y (since moves only east or north, so predecessors are (x-1,y) and (x,y-1)). In our loops, we process x from 1 to w and y from 1 to h. For a given (x,y), (x-1,y) is processed when x-1 was the outer loop index and all y were processed; that happens before we get to x in outer loop. And (x,y-1) is processed in the same outer loop x but earlier y because y increases. So yes, when we reach (x,y), dp[x][y] is fully computed from all incoming paths. Then we propagate to successors. So it's a standard DP over DAG with topological order.
Thus the DP is correct.
We must also handle the case when w=2, h>=2: our initialization sets dp[2][1][0][0]=1 and dp[1][2][1][0]=1. The DP loops will process (2,1) and (1,2) etc.
Now verify for w=2, h=3: Let's do quick mental to confirm DP yields 2.
We'll compute quickly: w=2, h=3.
Initialize: dp[2][1][0][0]=1; dp[1][2][1][0]=1.
Loop:
x=1,y=1: none.
x=1,y=2: dp[1][2][1][0]=1.
f=0, d=1. nd_list = both.
- nd=1 (north): to (1,3) valid. nf=0. So dp[1][3][1][0] +=1.
- nd=0 (east): to (2,2) valid. nf=1 (since 0!=1). dp[2][2][0][1] +=1.
x=1,y=3: dp[1][3][1][0]=1.
nd=1 north invalid (y=3,h=3? Actually h=3 means y goes up to 3, so y=3, y+1=4 >h => invalid). nd=0 east to (2,3) valid. nf=1. dp[2][3][0][1] +=1.
x=2,y=1: dp[2][1][0][0]=1.
nd=0 east invalid (x=2,w=2). nd=1 north to (2,2) valid. nf=1. dp[2][2][1][1] +=1.
x=2,y=2: states:
(0,1)=1 from earlier; (1,1)=1 from (2,1). Process:
- (d=0,f=1): must straight nd=0 east invalid; no move.
- (d=1,f=1): must straight nd=1 north to (2,3) valid, nf=0. dp[2][3][1][0] +=1.
x=2,y=3: states:
(0,1)=1 from (1,3); (1,0)=1 from (2,2). Process:
- (0,1): f=1, must straight nd=0 east invalid (x=2,w=2) so no move.
- (1,0): f=0, can choose nd=1 north invalid, nd=0 east invalid? Actually x=2,w=2 can't go east; north y=3,h=3 can't go north. So no moves. So dp[2][3] is final. Sum over states at (2,3): (0,1)=1, (1,0)=1 => total 2. That matches.
Thus DP works.
Now ensure modulo operation: We'll use mod = 100000.
Potential overflow: numbers can be up to maybe huge, but we take mod each addition. Python ints fine.
Now we need to handle multiple datasets up to 5, until w=h=0.
Now confirm sample: For 15 15 output 43688. Our DP should produce that.
Thus solution plan: implement DP as described.
But is there any more efficient combinatorial formula? Possibly, but DP is fine given constraints.
We should be careful about memory: we can use a 4D list in Python: dp = [[[[0]*2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)]? Actually careful indexing: dp[x][y][d][f] where x from 1..w, y from 1..h. So we can make a list of size (w+1) x (h+1) x 2 x 2. For performance, we could use a dictionary or numpy, but not needed.
Alternatively, we could compress dimensions: we only need to keep current row or something? Since we process in order, we could store entire grid; it's fine.
Now, some possible pitfalls: Since w and h are up to 100, we can allocate a 101x101x2x2 list. That's about 40k entries, each an integer, trivial.
We need to reset dp for each dataset.
Now, let's think about if there's any off-by-one: The intersection numbering: (1,1) start, (w,h) destination. Number of east steps = w-1, north steps = h-1. Our DP moves exactly those steps because we only move east/north and stop when we reach (w,h). The DP will generate all paths that end at (w,h). Since we only move in increasing directions, we will never overshoot. So correct.
Now we must ensure that we don't accidentally allow paths that exceed counts? The DP will stop at boundaries.
Now, check if the DP correctly enforces that after a turn you must go straight at the next intersection. In our state transition, when f=1 (meaning we turned at previous intersection), we force nd = d (straight). So that's correct.
Now, what about the flag for the initial moves? We set flag = 0 after first move because there was no turn at start. That's correct.
Now, what about the possibility that the first move could be considered a turn? It's not, so flag 0.
Now, what about the possibility of not moving at all? Not applicable because w,h >=2, so at least one move.
Thus DP is correct.
Now we need to output answer for each dataset.
But wait: The problem statement says: "A car that turns at an intersection cannot turn at the intersection immediately after that." It does not say that a car cannot turn at the intersection immediately before? So our interpretation stands.
One more nuance: Could the rule also apply to the starting intersection? For instance, if you start by going east, then north, is that considered a turn at intersection (1,1)? Usually not, because you didn't approach (1,1) from any road; you started there. So no.
Thus fine.
Now implement.
Implementation in Python:
def count_routes(w, h):
MOD = 100000
dp = [[[[0]2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)] # careful: indexing dp[x][y][d][f]
# But list comprehension order: w+1 lists, each containing h+1 lists, each containing 2 lists? Actually we need dp[x][y][d][f]. So we can define as 4D list:
# dp = [[[[0,0] for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)] ??? Let's think: We want first index x, second y, third d, fourth f. So we can create a 4D list using nested comprehensions.
# For simplicity, we can use a dictionary or a 3D list of objects? But easier: use a 4D list of size (w+1)(h+1)22.
# We'll do:
dp = [[[[0]*2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)]
# That gives: outer list of length w+1, each element is a list of length h+1, each of those is a list of length 2 (for d), each of those is a list of length 2 (for f). So dp[x][y][d][f] works.
# However, note that Python's list comprehensions will allocate correctly.
# Initialize
if w >= 2:
dp[2][1][0][0] = 1
if h >= 2:
dp[1][2][1][0] = 1
for x in range(1, w+1):
for y in range(1, h+1):
for d in (0,1):
for f in (0,1):
val = dp[x][y][d][f]
if val == 0:
continue
# determine next directions
if f == 1:
next_dirs = [d]
else:
next_dirs = [0,1]
for nd in next_dirs:
if nd == 0: # east
if x == w:
continue
nx, ny = x+1, y
else: # north
if y == h:
continue
nx, ny = x, y+1
nf = 1 if nd != d else 0
dp[nx][ny][nd][nf] = (dp[nx][ny][nd][nf] + val) % MOD
# sum over d,f at destination
ans = 0
for d in (0,1):
for f in (0,1):
ans = (ans + dp[w][h][d][f]) % MOD
return ans
But careful: In the loop, we used dp[x][y][d][f] where d and f are indices. But our dp is defined as dp[x][y][d][f] where the inner list for each (x,y) is a 2x2 array. The indexing dp[x][y][d][f] works if we defined correctly.
But note: The line dp = [[[[0]*2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)]:
- The outermost for: for _ in range(w+1) -> creates w+1 elements.
- For each, for _ in range(h+1) -> creates h+1 elements (y dimension). So dp[x] is a list of length h+1.
- For each (x,y), we create [[0]*2 for _ in range(2)] which creates a list of length 2 (for d), each element being a list [0,0] (for f). So dp[x][y] is a list of length 2, each being a list of length 2. So dp[x][y][d][f] works.
But careful: The order: dp[x][y][d][f] with d index first, then f. That's fine.
Alternatively, we could use a 3D array where each cell stores two states for (d,f) as a 2x2 matrix.
Now, a nuance: In the loops, we are iterating over all x,y including those that might be unreachable? But that's okay, val will be zero.
Now, test with sample.
We'll run mental test: w=3,h=4 gave 5. Good.
Now check w=15,h=15 should yield 43688 mod 100000. We'll trust.
Now edge cases: If w=2, h=100, etc. Should be fine.
Now confirm that the DP does not double count due to starting initialization: We set exactly one way for each initial move. That's correct.
Now, we might consider if there is a scenario where w=2 and h=2: both conditions true, we set both dp[2][1][0][0]=1 and dp[1][2][1][0]=1. Works.
Now, what about w=2, h=2: Are there any paths that start by going east then north, and also start by north then east? Both captured. So answer 2.
Now test w=2, h=3 gave 2.
Now test w=3, h=3? Let's manually compute some to verify consistency? Not needed.
Now ensure modulo: Since answer may be less than 100000, but we take mod.
Now, potential optimization: none needed.
Now, we should consider if there is any path that ends at (w,h) but the last move was a turn and the flag f for the destination state might be 1, but that's irrelevant.
Now, check if there is any scenario where we might incorrectly allow a turn after a turn due to our flag handling? Suppose f=1 (must go straight). We enforce nd = d. So cannot turn. After moving, we compute nf = 1 if nd != d else 0. Since nd == d, nf=0, so at the next intersection, turning is allowed again. That's correct: after going straight for one block, the restriction is lifted. Because the rule only prohibits turning at the immediate next intersection after a turn, not further.
Now, check if we need to enforce that after a turn you must go straight for at least one block, but if you go straight for one block and then turn again, that's allowed. So our DP allows patterns: turn, straight (one block), turn. That is allowed, as in the examples: e.g., allowed path E N N E N? Wait, we had path 3 disallowed because after turn we turned immediately. But turn, straight, turn is allowed. That matches our state: after turn (f=1), we force straight (nd=d). That moves one block. After that move, we arrive with nf=0. Then next we can turn. So pattern: direction A, turn to B (f becomes 1 at the next intersection), but at that next intersection we must go straight (so we go B again), then after that, we can turn. So pattern: A, B, B, C. That's turn, straight, turn. Good.
Now, what about the initial segment: can it be of length 1? Yes. For example, start with east then immediately turn north: that's allowed as long as after that turn we go at least one straight north. In our DP, start with east: we set dp[2][1][0][0]=1. At (2,1), f=0, we can turn north (nd=1) -> that's allowed because f=0. After moving north to (2,2), we set nf=1 (since turned). So at (2,2) we must go straight north next (or if destination, stop). So that path is allowed only if after the turn we go at least one more north (or reach destination). For example, if w=2, h=2, path E,N: after turn at (2,1), we go north to (2,2) and stop; that's allowed because we didn't need to go straight after turn because it's the destination. Our DP: from (2,1) we go north to (2,2) with nf=1. Since (2,2) is destination, we sum it. So path allowed. For w=2, h=3, path E,N,N: from (2,1) turn north to (2,2) with nf=1. Then at (2,2) (not dest), we must go straight: only north possible, so we go north to (2,3) with nf=0 (since nd=d). So that's allowed. So yes.
Now, what about path N,E for w=2,h=2: similarly allowed.
Now, what about path N,E,E for w=3,h=2: start north to (1,2) with flag0, then turn east to (2,2) with nf=1, then at (2,2) must go straight east to (3,2) with nf=0, destination. Allowed. So works.
Thus DP matches constraints.
Now complexity: O(wh4*2) = O(wh). For w,h up to 100, it's fine.
Now, could there be any issue with memory usage if we use a 4D list? 1011012*2 = 40,804 integers, fine.
Now we can code.
But before finalizing, let's double-check the indexing of the dp array: In Python, list comprehensions are evaluated eagerly. The expression:
dp = [[[[0]*2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)]
Let's break: The outermost for: for _ in range(w+1) => w+1 elements. For each such element, we create a list of length h+1. For each of those h+1, we create a list of length 2 (for d). That list is created by: [ [0]*2 for _ in range(2) ] which creates a list of two elements, each being [0,0]. So overall, dp[x][y] is a list of two elements (each a list of two ints). So dp[x][y][d][f] works.
But note: when we do dp[x][y][d][f] we need to ensure x and y are within bounds. We'll use 1-indexed.
We also need to mod after each addition. Since numbers can become large, but Python ints fine.
Now test with sample manually in code? Not now.
Now I'll consider if there's any alternative combinatorial interpretation that could simplify, but DP is straightforward.
Now one more nuance: The input says "w roads that extend straight in the north-south direction". That means there are w vertical roads. Intersections are at (i,j) where i from 1..w, j from 1..h. So indeed there are w vertical lines and h horizontal lines. Number of east steps = w-1, north steps = h-1. Good.
Now confirm for w=100, h=100, DP runs 10k cells * 4 states * maybe 2 transitions average => 80k operations, trivial.
Now we should think about the possibility of the number of datasets being up to 5, fine.
Now code in Python:
import sys
MOD = 100000
def solve():
lines = sys.stdin.read().strip().splitlines()
out = []
for line in lines:
if not line:
continue
w, h = map(int, line.split())
if w == 0 and h == 0:
break
# dp[x][y][d][f]
dp = [[[[0]*2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)]
if w >= 2:
dp[2][1][0][0] = 1
if h >= 2:
dp[1][2][1][0] = 1
for x in range(1, w+1):
for y in range(1, h+1):
for d in (0,1):
for f in (0,1):
val = dp[x][y][d][f]
if val == 0:
continue
if f == 1:
nd_options = [d]
else:
nd_options = [0,1]
for nd in nd_options:
if nd == 0:
if x == w:
continue
nx, ny = x+1, y
else:
if y == h:
continue
nx, ny = x, y+1
nf = 1 if nd != d else 0
dp[nx][ny][nd][nf] = (dp[nx][ny][nd][nf] + val) % MOD
ans = sum(dp[w][h][d][f] for d in (0,1) for f in (0,1)) % MOD
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
Now we should test with given example.
But before finalizing, we need to ensure that the DP initialization covers all cases when w=2 or h=2.
When w=2: we set dp[2][1][0][0]=1, and if h>=2, also dp[1][2][1][0]=1. That's fine.
When h=2: similarly.
When w=2,h=2: both set.
Now consider w=2, h=1? But constraints say h>=2, so not needed.
Now one more nuance: Starting moves could be both east and north; but what if w=2 and h=2, we set both, okay.
Now check w=100, h=100: DP loops 1001004 = 40k states, each maybe up to 2 transitions, 80k additions, fine.
Now, could there be a case where the number of ways exceeds 2^63? In Python, we are fine.
Now verify sample output: We'll test with a small script mentally? Already did for 3 4.
Now for 15 15 we trust.
But we should double-check with a known result? Could compute with DP manually? Not needed.
Now, is there any chance that the DP might double count due to the fact that we process dp[x][y][d][f] that may have been updated earlier in the same outer loop? Because we are only propagating forward, we never update dp[x][y] after we have processed it, because all incoming moves come from cells with x' <= x and y' <= y and at least one coordinate strictly less, so they have been processed earlier. But could there be a scenario where we propagate from (x,y) to (x,y+1) and then later in the same x but larger y we might later get an incoming contribution to (x,y) from (x-1,y) after we already processed (x,y)? Wait, (x-1,y) is processed when outer loop x-1 was processed, which is earlier than when we process x. So by the time we are at outer loop x, all cells with x-1 have been fully processed, including (x-1,y). So (x-1,y) already contributed to (x,y) before we started processing x. Because in the loops, when we were at x-1, we iterated over y from 1..h. So for a particular (x-1,y), we processed it and added to (x,y) at that time. Since we are now at x, the dp[x][y] value is already final from all incoming from left. Also from (x,y-1) it comes from same x but lower y, which was processed earlier in the inner loop. So when we reach (x,y), its value is complete. So the order is correct.
Thus DP is fine.
Now, one more nuance: In the initialization, we set dp[2][1][0][0]=1 and dp[1][2][1][0]=1 directly. But note that (2,1) and (1,2) will be processed later, and we will propagate from them. That's correct.
Now, think about any path that goes from (1,1) to (2,1) then maybe to (2,2) etc. All accounted.
Now, confirm that our DP does not include the case where the path length is zero? Not needed.
Now, about modulo: We'll take mod at each addition to keep numbers small.
Now, just to be safe, we might also apply mod when summing final answer, but we already did mod each addition, so sum will be within 4*100000 = 400000, but we still mod.
Now, final code as above.
But note: In the DP loops, we used "for x in range(1, w+1): for y in range(1, h+1):" This will process all cells, including (2,1) etc. But note that when x=2,y=1, dp[2][1][0][0] is set. Good.
Now, check for w=100, h=100, time is negligible.
Now, we should also consider that we might be able to optimize by using a 3D DP with states representing direction and whether the last move was a turn? But not needed.
Now, any pitfalls? Possibly the flag interpretation: We set flag f=1 when we are at a vertex and turning is prohibited. At a vertex, f=1 indicates that the previous vertex was a turn. This matches our state transition. But we need to ensure that we correctly set f for the initial states: we set f=0 for both initial moves. Is that correct? Let's think: At the intersection after the first move, e.g., (2,1) after moving east. The previous intersection was (1,1). Did we turn at (1,1)? No, because there was no incoming move. So f should be 0. Good.
Now, what about a scenario where we start with a move and then continue straight for a while? That's fine.
Now, check a case: Path: E,E,N,N,N (allowed). Let's trace DP: start (2,1)[0,0]=1. At (2,1) f=0, d=0, can go straight east to (3,1) with nf=0. So (3,1)[0,0] gets 1. At (3,1) (assuming w>3), then later go north etc. Works.
Now, check a path that has a turn after a straight segment of length 1? That's allowed. For example, path: E,N,N,E,N? That has a turn at (2,1), then straight N, then turn at (2,3)? Actually E,N,N,E,N: at (2,1) turn, then at (2,2) straight (since must), then at (2,3) we turn to E? But we turned at (2,3) from N to E. That is allowed because after the turn at (2,1), we went straight at (2,2), then at (2,3) we turned. Our DP: (2,1)[0,0]=1 -> turn north: to (2,2)[1,1]=1. At (2,2) f=1,d=1: must straight north to (2,3)[1,0]=1. At (2,3) f=0,d=1: can turn east: to (3,3)[0,1]=1. So allowed. So pattern turn, straight, turn is okay. That matches earlier.
Now, what about a turn, then straight for two blocks, then turn? That's also allowed. DP handles.
Now, check a path that has a turn at the very last intersection? That is, you turn into the destination. For example, path to (w,h) where you approach from south and then turn east to reach? Actually if you arrive at (w-1, h) and then go east to (w, h), that is a turn if previous was north? But if you were going north along column w-1, then at (w-1, h) you go east to (w, h). That is a turn (north->east). But after turning, you immediately stop, which is allowed. In DP, from state at (w-1, h) with d=1 (north), f maybe 0, we can choose nd=0 (east) to go to (w,h). That's allowed regardless of f? If f=1, then must go straight, but if we are at (w-1, h) and f=1, we would be forced to go north, but north would be out of bounds because y=h, so we cannot go straight; but could we turn? No, because f=1 prohibits turning. So such a path would be invalid if it required a turn at the last step but f=1. But is that scenario possible? Could we arrive at (w-1, h) with f=1? That would mean we turned at previous intersection (w-1, h-1?). Let's see: If we turned at (w-1, h-1) to go north, then at (w-1, h) we would be forced to go straight north, but can't. So such path would be impossible. So DP correctly disallows that. That seems correct: after a turn, you must go straight for at least one block. If that straight block would go out of bounds (i.e., you'd reach destination by going straight, that's okay), but if you need to turn to reach destination, you can't because you must go straight, but straight is impossible (out of bounds) or would overshoot, so that path is invalid. So DP enforces that you cannot turn at the immediate next intersection, so if the only way to reach destination from current position is to turn and you are prohibited from turning, then that path is not counted. So DP correctly counts only allowed paths.
Thus DP is sound.
Now, we can implement.
But we need to be careful about modulo: The problem says "remainder of JOI's number of commuting routes divided by 100000". So we output the integer remainder. They didn't specify leading zeros, just integer.
Now, the sample input:
3 4
15 15
0 0
Output:
5
43688
We'll produce that.
Now, I'll code accordingly.
However, I noticed one thing: In the DP loops, we process (x,y) in order from 1..w and 1..h. But for (x,y) that are destination (w,h), we still process them? But we still propagate from (w,h)? However, from (w,h) we cannot move further because x==w and y==h, so all moves are invalid. So we can skip propagating, but it's okay as we check bounds. However, processing (w,h) might still have some dp value and we will attempt to propagate, but all moves will be invalid due to bounds, so no effect. So it's fine.
Now, we can optionally optimize by breaking early when x==w and y==h, but not necessary.
Now, about modulo: We'll do modulo each addition; Python's mod is fine.
Now, let's also consider using memory more efficiently by using a 3D array with dimensions (w+1)*(h+1)*4 where we encode (d,f) as a single index 0..3. But not necessary.
Now, final code.
But wait: The input includes multiple datasets, and the termination condition is "0 0". So we read lines, break when both zero.
Now, test for extreme: w=100, h=100. The DP will run and answer should be computed quickly.
Now, I realize that the DP uses nested loops that go through all y for each x. That's fine.
Now, one more nuance: In the DP, we used "dp = [[[[0]*2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)]". This creates a list where the innermost [0]*2 creates a list of two zeros. However, we need to ensure that each inner list is independent. Since we use list comprehension, it's fine.
Let's test quickly in mind: a = [[0]*2 for _ in range(2)] creates two separate lists [0,0] and [0,0], each independent. So good.
Now, ensure that for each dataset we reinitialize dp.
Now, implement.
One more thing: Since we are using 1-indexed coordinates, we need to ensure that dp indices are within range. w+1 is up to 101, h+1 up to 101, fine.
Now, we also need to be careful about Python recursion? Not used.
Now, code.
But before finishing, I'll double-check the transition for the flag: nf = 1 if nd != d else 0. But what about the case where we are at a cell and f=1, we only allowed nd=d, so nd == d, so nf=0. That's correct: after going straight for one block after a turn, the restriction is lifted.
Now, what about the case where f=0 and we choose nd = d (straight). Then nf = 0. That's correct: no turn, so next intersection no restriction.
Now, what about f=0 and we choose nd != d (turn). Then nf = 1: we turned at this intersection, so next intersection we cannot turn.
Thus correct.
Now, final code as above.
But we should also test a couple more small cases manually with DP to ensure no off-by-one:
Case w=2,h=3 => answer 2 (we did)
Case w=3,h=2 => answer 2 (by symmetry) Let's quickly compute with DP? Should be 2.
Case w=3,h=3: Let's manually compute maybe? Not needed, but we can test after coding.
Now, also note that the DP counts paths that end at (w,h) regardless of the flag. That's correct because at destination you stop, so no further restriction.
Now, is there any situation where a path may arrive at (w,h) but with flag f=1? That's fine.
Now, think about the possibility of an alternative interpretation: The rule "A car that turns at an intersection cannot turn at the intersection immediately after that" might also forbid turning at the intersection immediately before? No.
Thus solution is good.
Now, implement.
Potential improvement: Use a 2D array of objects with 4 states. But it's fine.
Now, we need to ensure we handle the case where w=2 and h>=2: our initialization sets dp[2][1][0][0]=1. In the loops, we will process (2,1) etc. Also, we set dp[1][2][1][0]=1. So both initial moves are accounted. If w=2 and h=2, both are set.
Now, is there any path that goes from (1,1) to (2,1) to (2,2) (east then north) and also from (1,1) to (1,2) to (2,2) (north then east). Both counted. Good.
Now, all good.
Now, I will write the final solution.
But before finalizing, consider a more efficient method: We could use matrix exponentiation? Not needed.
Now, let's also think about whether the DP might have an issue with overlapping subproblems? Already considered.
Now, test with w=4, h=4 maybe? Not needed.
Now, one more check: In the DP, when x and y are both 1, we have no states. So we skip.
Now, is there any path that starts by going east and then east again? That is allowed. It will be generated: from (2,1) we can go east to (3,1) with nd=0, f=0 -> nf=0, etc.
Now, fine.
Now, answer.
But note: The problem is from JOI (Japanese Olympiad in Informatics). Usually they expect a DP solution or combinatorial formula. With w,h up to 100, DP is expected.
Now, consider potential integer overflow in other languages but Python fine.
Now, final answer: code as described.
Now, check sample: 15 15 => 43688. Our DP should produce that. Let's quickly think if any other nuance: Could there be a case where the number of routes modulo 100000 is 0? Possibly, but we output 0.
Now, ensure that we output each answer on its own line.
Now, write final solution.
But before final, I'll test with some small values by mental enumeration or using a small script? Since I cannot run here, I'll manually compute for w=3,h=3 to see if DP gives something plausible.
We'll enumerate manually for 3x3 grid (2 east, 2 north steps). Total unrestricted paths = C(4,2)=6.
List all paths:
- E E N N
- E N E N
- E N N E
- N E E N
- N E N E
- N N E E
Now apply restriction: no two consecutive turns.
Identify turns:
- E E N N: changes: E->E no, E->N turn, N->N no -> one turn allowed.
- E N E N: moves: E,N,E,N. Turns: E->N turn, N->E turn, E->N turn -> consecutive turns at vertices after move1 and move2 (E->N then N->E) and also after move2 and move3? Actually check: vertices: after move1 (E->N) turn, after move2 (N->E) turn -> consecutive turns -> forbidden.
- E N N E: moves: E,N,N,E. Turns: E->N turn, N->N no, N->E turn -> allowed? Check immediate after turn: after turn at E->N, at next vertex after move2 (N->N) no turn, so okay. Turn at N->E is allowed because previous vertex had no turn. So allowed.
- N E E N: moves: N,E,E,N. Turns: N->E turn, E->E no, E->N turn -> allowed.
- N E N E: moves: N,E,N,E. Turns: N->E turn, E->N turn, N->E turn -> consecutive turns at first two vertices -> forbidden.
- N N E E: moves: N,N,E,E. Turns: N->N no, N->E turn, E->E no -> allowed.
Thus allowed: 1,3,4,6 => 4 paths.
Let's test DP quickly: w=3,h=3.
Initialize: (2,1)[0,0]=1; (1,2)[1,0]=1.
We'll compute DP quickly:
We'll list states as we go.
I'll use a systematic approach.
We'll create a table of dp for each cell with states (d,f) counts.
Coordinates (x,y). I'll compute step by step.
Initialize:
(2,1): (0,0)=1
(1,2): (1,0)=1
Now iterate x=1..3, y=1..3.
x=1,y=1: none.
x=1,y=2: (1,0)=1
f=0,d=1 -> nd can be 0,1.
- nd=1 (north): to (1,3). nf=0 -> (1,3)[1,0]+=1
- nd=0 (east): to (2,2). nf=1 -> (2,2)[0,1]+=1
x=1,y=3: now (1,3)[1,0]=1
nd=1 invalid (north out), nd=0 east to (2,3). nf=1 -> (2,3)[0,1]+=1
x=2,y=1: (2,1)[0,0]=1
nd=0 east to (3,1). nf=0 -> (3,1)[0,0]+=1
nd=1 north to (2,2). nf=1 -> (2,2)[1,1]+=1
x=2,y=2: states: (0,1)=1, (1,1)=1
Process (0,1): f=1 => must go straight nd=0. East to (3,2). nf=0 -> (3,2)[0,0]+=1
Process (1,1): f=1 => must go straight nd=1. North to (2,3). nf=0 -> (2,3)[1,0]+=1
x=2,y=3: states: (0,1)=1 (from (1,3)), (1,0)=1 (from (2,2) second)
Process (0,1): f=1 => must straight nd=0. East to (3,3). nf=0 -> (3,3)[0,0]+=1
Process (1,0): f=0 => can nd=1 or 0.
nd=1 north invalid (y=3,h=3). nd=0 east to (3,3). nf=1 (since nd!=d) -> (3,3)[0,1]+=1
x=3,y=1: (3,1)[0,0]=1
nd=0 east invalid; nd=1 north to (3,2). nf=1 -> (3,2)[1,1]+=1
x=3,y=2: states: (0,0)=1, (1,1)=1
Process (0,0): f=0 => can nd=0 or 1. nd=0 invalid (east). nd=1 north to (3,3). nf=1 -> (3,3)[1,1]+=1
Process (1,1): f=1 => must straight nd=1. North to (3,3). nf=0 -> (3,3)[1,0]+=1
x=3,y=3: states: (0,0)=1, (0,1)=1, (1,1)=1, (1,0)=1? Let's sum:
(0,0): from (2,3) first? Actually (2,3) gave (3,3)[0,0] =1.
(0,1): from (2,3) second gave (3,3)[0,1] =1.
(1,1): from (3,2) first gave (3,3)[1,1] =1.
(1,0): from (3,2) second gave (3,3)[1,0] =1.
So total = 4. Good.
Thus DP yields 4 for 3x3, matches our manual count.
So DP works.
Now for 15 15, we trust.
Now finalize.
One more thing: The modulo is 100000, which is not a prime, but we only do addition and mod, no division.
Now, we could possibly use a more compact DP by noting symmetry, but fine.
Now, code is ready.
But ensure we handle reading input lines with possible extra spaces. We'll split and map.
Now, I'll produce final answer.
But I want to consider if there is any scenario where the DP might incorrectly count a path that has a turn at the first intersection? Our initialization sets flag 0 after first move, so at (2,1) or (1,2), we can turn. That's correct because turning at (1,1) is not considered a turn (no incoming). So first actual turn can occur at the intersection after the first move. That's allowed.
Now, what about a path that starts with two same direction moves, then a turn? That's fine.
Now, all good.
Now, I'll write the final solution in the answer.
But also note: The problem says "The number of data sets does not exceed 5." So at most 5.
Now, code:
def solve():
import sys
MOD = 100000
out_lines = []
for line in sys.stdin:
line = line.strip()
if not line:
continue
w, h = map(int, line.split())
if w == 0 and h == 0:
break
# dp[x][y][d][f]
dp = [[[[0]*2 for _ in range(2)] for _ in range(h+1)] for _ in range(w+1)]
if w >= 2:
dp[2][1][0][0] = 1
if h >= 2:
dp[1][2][1][0] = 1
for x in range(1, w+1):
for y in range(1, h+1):
for d in (0,1):
for f in (0,1):
val = dp[x][y][d][f]
if val == 0:
continue
if f == 1:
nd_list = [d]
else:
nd_list = [0,1]
for nd in nd_list:
if nd == 0:
if x == w:
continue
nx, ny = x+1, y
else:
if y == h:
continue
nx, ny = x, y+1
nf = 1 if nd != d else 0
dp[nx][ny][nd][nf] = (dp[nx][ny][nd][nf] + val) % MOD
ans = sum(dp[w][h][d][f] for d in (0,1) for f in (0,1)) % MOD
out_lines.append(str(ans))
sys.stdout.write("\n".join(out_lines))
Now, in Python, this should run quickly.
But we might want to avoid deep nesting for readability, but it's okay.
Now, check memory: dp size: (w+1)(h+1)22 ints. For w=h=100, that's 101101*4 = 40804 ints. Each int maybe 28 bytes => ~1.1 MB, fine.
Now, final answer.
Now, I'm going to produce the final response with the code and explanation.
But the user asked "problem" and we need to output a program. We'll provide the solution code and explanation.
Thus answer will include the code in Python.
I'll ensure it's properly formatted.
Now, let's produce final answer. ```python
import sys
MOD = 100000
def solve() -> None:
out = []
for line in sys.stdin:
line = line.strip()
if not line:
continue
w, h = map(int, line.split())
if w == 0 and h == 0:
break
# dp[x][y][d][f] (1-indexed, d:0=east,1=north, f:0=free,1=must go straight)
dp = [[[[0] * 2 for _ in range(2)] for _ in range(h + 1)] for _ in range(w + 1)]
# first move from (1,1)
if w >= 2:
dp[2][1][0][0] = 1 # go east
if h >= 2:
dp[1][2][1][0] = 1 # go north
for x in range(1, w + 1):
for y in range(1, h + 1):
for d in (0, 1):
for f in (0, 1):
cur = dp[x][y][d][f]
if cur == 0:
continue
# possible next directions
if f == 1: # just turned, must go straight
nd_list = [d]
else:
nd_list = [0, 1] # free to go straight or turn
for nd in nd_list:
if nd == 0: # east
if x == w:
continue
nx, ny = x + 1, y
else: # north
if y == h:
continue
nx, ny = x, y + 1
nf = 1 if nd != d else 0 # turn at current intersection?
dp[nx][ny][nd][nf] = (dp[nx][ny][nd][nf] + cur) % MOD
ans = sum(dp[w][h][d][f] for d in (0, 1) for f in (0, 1)) % MOD
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
solve()