[Question]
  [
Your task is to assemble the integers from `1` to `N` (given as input) into a rectangle of width `W` and height `H` (also given as input). Individual numbers may be rotated by any multiple of 90 degrees, but they must appear as contiguous blocks in the rectangle. That is, you cannot break one of the numbers into multiple digits and place the digits in the rectangle individually, neither can you bend three digits of a number around a corner. You could consider each number a brick from which you're building a wall.
Here is an example. Say your input is `(N, W, H) = (12, 5, 3)`. One possible solution is:
```
18627
21901
53114
```
For clarity, here are two copies of this grid, one with the one-digit numbers hidden and one with the two-digit numbers hidden:
```
1####    #8627
2##01    #19##
##11#    53##4
```
It's fine if the rectangle cannot be disassembled again in a unique way. For instance, in the above example, the `12` could also have been placed like this:
```
#####    18627
21#01    ##9##
##11#    53##4
```
## Rules
You may assume that `N` is positive and that `W*H` matches the number of digits in the integers from `1` to `N` inclusive, and that a tiling of the rectangle into the given numbers exists. I don't currently have a proof whether this is always possible, but I'd be interested in one if you do.
Output may either be a single linefeed-separated string or a list of strings (one for each line) or a list of lists of single-digit integers (one for each cell).
The results of your submission must be determinstic and you should be able to handle all test cases in less than a minute on reasonable desktop machine.
You may write a [program or a function](http://meta.codegolf.stackexchange.com/q/2419) and use any of the our [standard methods](http://meta.codegolf.stackexchange.com/q/2447) of receiving input and providing output.
You may use any [programming language](http://meta.codegolf.stackexchange.com/q/2028), but note that [these loopholes](http://meta.codegolf.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden by default.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest valid answer – measured in *bytes* – wins.
## Test Cases
Except for the first one, none of these are unique. Each test case is `N W H` followed by a possible output. Make sure that your answer works when the rectangle is too narrow to write the larger numbers horizontally.
```
1 1 1
1
6 6 1
536142
6 2 3
16
25
34
10 1 11
1
0
8
9
2
6
7
3
1
5
4
11 13 1
1234567891011
27 9 5
213112117
192422581
144136119
082512671
205263272
183 21 21
183116214112099785736
182516114011998775635
181116013911897765534
180415913811796755433
179115813711695745332
178315713611594735231
177115613511493725130
176215513411392715029
175115413311291704928
174115313211190694827
173115213111089684726
172015113010988674625
171915012910887664524
170814912810786654423
169714812710685644322
168614712610584634221
167514612510483624120
166414512410382614019
165314412310281603918
164214312210180593817
163114212110079583716
200 41 12
81711132917193661114105533118936111184136
50592924448815915414562967609909953662491
89529721161671582389717813151113658811817
41418184511110119010183423720433017331118
35171183614003547461181197275184300111711
41874381132041861871718311415915921116264
11914245014112711011594492626831219331845
17125112629222085166344707736090956375181
94507611291431121128817413566319161275711
11011540021119913511011169939551729880780
92725141607727665632702567369893534277304
78118311405621148296417218591118562161856
```
  
[Answer]
# Python 2, ~~210~~ 200 bytes
**Edit:** Works now!
Fills from top to bottom, left to right, starting with the largest numbers. Then, transpose and do it again. Then transpose and print. I had to pad with spaces for the transposition to work, since the lines aren't to their full length yet.
I had trouble getting a nested `exec` working (to do `exec'exec"..."*w\n;...'*2`. If someone can figure it out, let me know.
```
n,w,h=input()
s=[""]*h
for x in 1,2:
    exec"for i in range(h):l=len(s[i]+`n`)<=w;s[i]+=`n`*l;n-=l\n"*w;s=[r.replace(" ","")for r in map(lambda x:`x`[2::5],zip(*[r.ljust(w)for r in s]))];w,h=h,w
print s
```
[**Try it online**](http://ideone.com/NgUF2x) - Uses a modified function so it can run multiple test cases more easily (and it couldn't use `exec`). Uncomment the other version and modify stdin to see it run.
**Less golfed:**
```
def f(n,w,h):
    s=[""]*h
    for x in 1,2:
        for j in[0]*w:
            for i in range(h):
                l=len(s[i]+`n`)<=w
                s[i]+=`n`*l
                n-=l
        s=[r.ljust(w)for r in s]
        s=map(lambda x:`x`[2::5],zip(*s))
        s=[r.replace(' ','')for r in s]
        w,h=h,w
    print"\n".join(s)
```
[Answer]
## JavaScript, ~~284~~ ~~259~~ ~~245~~ ~~241~~ ~~240~~ ~~223~~ ~~209~~ 205 bytes
```
// Golfed
let f = (N,W,H)=>eval('a=Array(H).fill("");while(N)g:{s=""+N--;d=s[L="length"];for(i in a)if(a[i][L]+d<=W){a[i]+=s;break g}for(p=0;d;++p){l=a[p][L];for(k=p+d;k>p;)l=a[--k][L]-l?W:l;while(l<W&&d)a[p+--d]+=s[d]}}a');
// Ungolfed
(N,W,H) => {
    a = Array(H).fill(""); // Create `H` empty rows.
    while (N) g : {
        s = "" + N--; // Convert the number to a string.
        d = s[L="length"]; // Count the digits in the number.
        // Loop through the rows trying to fit the number in horizontally.
        for (i in a) {
            if (a[i][L] + d <= W) { // If it fits.
                a[i] += s; // Append the number to the row.
                break g; // This is what a goto statement looks like in JavaScript.
            }
        }
        // Loop through the rows trying to fit the number in vertically.
        for (p = 0; d; ++p) {
            l = a[p][L]; // Get the length of the row.
            // Find `d` adjacent rows of the same length.
            for (k = p + d; k > p; ) {
                // If `a[--k][L] == l`, continue.
                // Else set `l` to `W` so the next loop doesn't run.
                l = a[--k][L] - l ? W : l;
            }
            // Put the characters in position.
            while (l < W && d)
                a[p+--d] += s[d];
        }
    }
    return a;
}
let test_data = [[1,1,1],
                 [6,6,1],
                 [6,2,3],
                 [10,1,11],
                 [10,11,1],
                 [11,13,1],
                 [27,9,5],
                 [183,21,21],
                 [184,2,222],
                 [200,41,12],
                 [1003,83,35]];
for (let test of test_data)
    console.log(f(test[0],test[1],test[2]));
```
[Answer]
# Pyth, 35 bytes
```
juum+WghHl+dQd~tQN<+.TGmkeHeH)_BvzY
```
Credits to mbomb007. I used his algorithm. 
Originally I only wanted to help Steven H., but then I really wanted to see a short version. 
Takes `N` on the first line, and `W,H` on the second line: Try it online: [Demonstration](http://pyth.herokuapp.com/?code=juum%2BWghHl%2BdQd~tQN%3C%2B.TGmkeHeH%29_BvzY&input=10%0A1%2C%2011&test_suite_input=6%0A1%2C%206%0A6%0A6%2C%201&debug=0&input_size=2)
Found a nasty bug in the Pyth implementation of `.[` (my own fault, since I implemented it). Gotta fix it tomorrow. This resulted in +3 bytes. 
### Explanation:
```
juum+WghHl+dQd~tQN<+.TGmkeHeH)_BvzY
                                  Y   start with the empty list []
                                      I'll perform all operations on this list. 
                                      Sometimes it is called G, sometimes N. 
                                vz    read the second line and evaluate it: [W, H]
                              _B      bifurcate it with reverse: [[W, H], [H, W]]
 u                                    for each pair H in ^:
                    .TG                  transpose G
                   +   mkeH              append H[1] empty strings
                  <        eH            use only the first H[1] strings
                                         lets call this result N
  u                          )           modify N, until it doesn't change anymore:
   m                        N               map each d in N to:
     WghHl+dQ                                  if H[0] >= len(d+Q):
    +        d  Q                                 d + Q
              ~t                                  and decrement Q by 1
             d                                 else:
                                                  d
j                                     at the end print every row on a separate line
```
[Answer]
# Pyth, ~~79 50~~ 48 Bytes
~~Noncompeting until I work out bugs (for example, [6,6,1] returns the same as [6,1,6]). It's my first time trying to use Pyth, so I'm probably missing a lot of features.~~
*Thanks to Jakube, saved 29 bytes and made my code actually work!*
Saved another two byte by realizing that the `repr()` calls were unnecessary.
It's basically just a translation of mbomb007's Python 2 answer.
```
AEJmkHFb2VHVGIgGl+@JNQ XNJ~tQ)))=.[.TJkGA,HG)jJ
```
Takes input in the form of  
`n`  
`w,h`.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 27 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
é!L↑?∞S░♠╔)¥¼/ÿµ◄÷│♦╫Δò6√√╣
```
[Run and debug it](https://staxlang.xyz/#p=82214c183fec53b006c9299dac2f98e611f6b304d7ff9536fbfbb9&i=12+3+5%0A1+1+1%0A6+1+6%0A6+3+2%0A10+11+1%0A11+1+13%0A27+5+9%0A183+21+21%0A200+12+41&a=1&m=2)
It takes input on one line in the for `{N} {H} {W}`.
This program starts with a grid of spaces of the specified size. For each number from `N` .. `1`, it attempts to do a single string replacement from a string of spaces of the appropriate size to the formatted number. If no replacement can be performed, then it tries again with a transposed grid.
```
z)A+*   create "grid" of spaces and newlines of specified size
,Rr     create range [n ... 1]
F       for each number execute the indented section; it will fit the value into the grid
  $%z(  make a string out of spaces the same length as the number; e.g. 245 => "   "
  Y     store the space string in register Y; this will be used as a search substring
  [#    count the number of occurrences of y in the grid; the grid is still on the stack
  X     store the count in register X; this will be used as a condition
  G     jump to routine at closing curly brace
  y_$|e replace the first instance of y (the spaces) with the current number
  xG    push x; then jump to routine at closing curly brace
        end program
}       called routine jump target
C       pop top of stack; if it's truthy terminate routine
|jM|J   split on newlines; transpose; join with newlines
```
[Run this one](https://staxlang.xyz/#c=z%29A%2B*++%09create+%22grid%22+of+spaces+and+newlines+of+specified+size%0A,Rr++++%09create+range+[n+...+1]%0AF++++++%09for+each+number+execute+the+indented+section%3B+it+will+fit+the+value+into+the+grid%0A++%24%25z%28+%09make+a+string+out+of+spaces+the+same+length+as+the+number%3B+e.g.+245+%3D%3E+%22+++%22%0A++Y++++%09store+the+space+string+in+register+Y%3B+this+will+be+used+as+a+search+substring%0A++[%23+++%09count+the+number+of+occurrences+of+y+in+the+grid%3B+the+grid+is+still+on+the+stack%0A++X++++%09store+the+count+in+register+X%3B+this+will+be+used+as+a+condition%0A++G++++%09jump+to+routine+at+closing+curly+brace%0A++y_%24%7Ce%09replace+the+first+instance+of+y+%28the+spaces%29+with+the+current+number%0A++xG+++%09push+x%3B+then+jump+to+routine+at+closing+curly+brace%0A+++++++%09end+program%0A%7D++++++%09called+routine+jump+target%0AC++++++%09pop+top+of+stack%3B+if+it%27s+truthy+terminate+routine%0A%7CjM%7CJ++%09split+on+newlines%3B+transpose%3B+join+with+newlines&i=12+3+5%0A1+1+1%0A6+1+6%0A6+3+2%0A10+11+1%0A11+1+13%0A27+5+9%0A183+21+21%0A200+12+41&a=1&m=2)
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 59 bytes
Oy, Pip can be verbose for a golfing language sometimes...
```
YsXbRLcJnWa{T` `X#a~yx:Y(t:{J*Z:a^n}Jny)Y$`.a.$'--a}x?(ty)y
```
[Try it online!](https://tio.run/##K8gs@P8/sjgiKcgn2SsvPLE6JEEhIUI5sa6ywipSo8Sq2ksryioxLq/WK69SM1IlQS9RT0VdVzextsJeo6RSs/L///@Wlv8t/xsZAgA "Pip – Try It Online")
### Ungolfed
Approach: store the grid as a newline-separated string, using regex to replace a run of spaces with the current number, and transposing if there isn't a run of spaces long enough.
```
; Command-line arguments are: a (the number), b (the width), c (the height)
; x is "", a falsey value
; Set up the initial string:
;  sXb  String of b spaces
;  RLc  Make a list of c of those
;  Jn   Join on newline
Y sXb RL c J n
; Define a function t that transposes a newline-separated string:
;  a^n  Split argument on newlines
;  Z    Zip (transposes, but results in a list of lists of characters)
;  J*   Join each of the sublists into a string
;  Jn   Join those strings on newline
t: {(J* Z (a^n)) J n}
; Loop while a is nonzero:
W a {
  ; Loop till a regex made of len(a) spaces matches in y:
  T y ~ ` `X#a
    ; Transpose y and set x to a truthy value to indicate that we've transposed it:
    ;  (t y)  Call the t function on y
    ;  Y      Assign the result back to y
    ;  x:     Assign it to x also
    x: Y (ty)
  ; When we break out of the loop, we have matched the first run of len(a) spaces
  ; in y; now we want to replace that with the number a:
  ;  $`   The part of the string before the match
  ;  .a   Concatenated to a
  ;  .$'  Concatenated to the part of the string after the match
  ;  Y    Assign that to y
  Y $`.a.$'
  ; Decrement a
  --a
}
; After the main loop, we need to transpose y back if we transposed it:
;  x?     Is x truthy?
;  (t y)  If so, transpose y
;  y      If not, return y as-is
x ? (ty) y
; The result is autoprinted.
```
]